Connecting to databases (SQL Server, MySQL, etc.) MCQs ASP.NET

What namespace is commonly used for connecting to SQL Server in ASP.NET?
a. System.Data.SqlClient
b. System.Data.SqlServer
c. System.Data.MySqlClient
d. System.Data.OleDb

Answer: a. System.Data.SqlClient

Which class is used to create a connection to a SQL Server database?
a. SqlConnection
b. SqlCommand
c. SqlDataAdapter
d. SqlDataReader

Answer: a. SqlConnection

What method of SqlConnection is used to open a connection to a database?
a. Open()
b. Start()
c. Connect()
d. Initialize()

Answer: a. Open()

Which class is used to execute SQL commands against a SQL Server database?
a. SqlCommand
b. SqlDataAdapter
c. SqlConnection
d. SqlDataReader

Answer: a. SqlCommand

What property of SqlCommand specifies the SQL statement or stored procedure to execute?
a. CommandText
b. CommandType
c. CommandCommand
d. CommandQuery

Answer: a. CommandText

What is the purpose of SqlDataAdapter in ADO.NET?
a. To fill a DataSet or DataTable with data from a database
b. To execute SQL commands
c. To manage database connections
d. To create database schema

Answer: a. To fill a DataSet or DataTable with data from a database

How do you retrieve data from a SQL Server database using SqlDataReader?
a. By using Read() method in a loop
b. By using ExecuteReader() method
c. By using Fill() method
d. By using GetData() method

Answer: a. By using Read() method in a loop

Which method of SqlCommand executes a SQL command and returns the number of rows affected?
a. ExecuteNonQuery()
b. ExecuteScalar()
c. ExecuteReader()
d. ExecuteDataSet()

Answer: a. ExecuteNonQuery()

What method is used to execute a SQL query that returns a single value?
a. ExecuteScalar()
b. ExecuteReader()
c. ExecuteNonQuery()
d. ExecuteDataSet()

Answer: a. ExecuteScalar()

Which class is used to work with MySQL databases in ASP.NET?
a. MySqlConnection
b. SqlConnection
c. OleDbConnection
d. OracleConnection

Answer: a. MySqlConnection

What namespace is used for connecting to MySQL databases in .NET?
a. MySql.Data.MySqlClient
b. System.Data.MySqlClient
c. MySql.Data.SqlClient
d. System.Data.OracleClient

Answer: a. MySql.Data.MySqlClient

Which method of MySqlConnection is used to open a connection to a MySQL database?
a. Open()
b. Start()
c. Connect()
d. Initialize()

Answer: a. Open()

What is the purpose of the ConnectionString property in a SqlConnection or MySqlConnection object?
a. To specify the database server, database name, and authentication information
b. To define the SQL command to execute
c. To set the database schema
d. To open the database connection

Answer: a. To specify the database server, database name, and authentication information

Which method of SqlDataAdapter is used to update the database with changes made in a DataSet?
a. Update()
b. Insert()
c. Save()
d. Commit()

Answer: a. Update()

What property of SqlCommand specifies the type of command being executed?
a. CommandType
b. CommandText
c. CommandCommand
d. CommandQuery

Answer: a. CommandType

How do you execute a stored procedure using SqlCommand?
a. Set CommandType to StoredProcedure and set CommandText to the stored procedure name
b. Set CommandType to Text and set CommandText to the stored procedure name
c. Use ExecuteReader() method with a stored procedure name
d. Use ExecuteScalar() method with a stored procedure name

Answer: a. Set CommandType to StoredProcedure and set CommandText to the stored procedure name

Which class provides a mechanism for reading a forward-only stream of rows from a SQL Server database?
a. SqlDataReader
b. SqlCommand
c. SqlDataAdapter
d. SqlConnection

Answer: a. SqlDataReader

What is the purpose of the Transaction property in SqlConnection?
a. To manage transactions for database operations
b. To specify the SQL command to execute
c. To define the database schema
d. To open and close the connection

Answer: a. To manage transactions for database operations

Which method of SqlTransaction is used to commit the transaction?
a. Commit()
b. Complete()
c. Finalize()
d. Apply()

Answer: a. Commit()

How can you roll back a transaction in ADO.NET?
a. By using the Rollback() method of SqlTransaction
b. By using the Undo() method of SqlConnection
c. By using the Revert() method of SqlDataAdapter
d. By using the Cancel() method of SqlCommand

Answer: a. By using the Rollback() method of SqlTransaction

What is the default value for the ConnectionTimeout property in a SqlConnection object?
a. 15 seconds
b. 30 seconds
c. 60 seconds
d. 90 seconds

Answer: a. 15 seconds

Which method is used to execute a batch of SQL commands in ADO.NET?
a. ExecuteNonQuery()
b. ExecuteReader()
c. ExecuteScalar()
d. ExecuteBatch()

Answer: a. ExecuteNonQuery()

How do you close a database connection in ADO.NET?
a. By calling the Close() method of the SqlConnection or MySqlConnection object
b. By calling the Disconnect() method
c. By setting the ConnectionString property to null
d. By using the Dispose() method

Answer: a. By calling the Close() method of the SqlConnection or MySqlConnection object

Which method of SqlDataAdapter fills a DataSet with data from the database?
a. Fill()
b. Load()
c. Populate()
d. Insert()

Answer: a. Fill()

How do you add parameters to a SqlCommand object?
a. By using the Parameters.Add() method
b. By using the AddParameter() method
c. By using the SetParameter() method
d. By using the InsertParameter() method

Answer: a. By using the Parameters.Add() method

What class is used to work with data in a disconnected manner in ADO.NET?
a. DataSet
b. SqlConnection
c. SqlDataReader
d. SqlCommand

Answer: a. DataSet

What does DataTable represent in ADO.NET?
a. A single table of in-memory data
b. A collection of tables
c. A connection to a database
d. A command to execute

Answer: a. A single table of in-memory data

Which ADO.NET object can be used to bind a DataSet or DataTable to a GridView control in ASP.NET?
a. SqlDataAdapter
b. SqlCommand
c. SqlConnection
d. SqlDataReader

Answer: a. SqlDataAdapter

What is the role of the CommandType property in a SqlCommand object?
a. To specify whether the command is a SQL query, stored procedure, or table direct
b. To specify the SQL query to execute
c. To set the connection string
d. To set the timeout value

Answer: a. To specify whether the command is a SQL query, stored procedure, or table direct

How can you retrieve a scalar value from a database in ADO.NET?
a. By using ExecuteScalar() method of SqlCommand
b. By using ExecuteReader() method
c. By using ExecuteNonQuery() method
d. By using ExecuteDataSet() method

Answer: a. By using ExecuteScalar() method of SqlCommand

Which property of SqlDataAdapter is used to specify the SQL commands for selecting, inserting, updating, and deleting data?
a. SelectCommand, InsertCommand, UpdateCommand, DeleteCommand
b. CommandText, InsertText, UpdateText, DeleteText
c. DataSource, InsertSource, UpdateSource, DeleteSource
d. SqlCommand, InsertCommand, UpdateCommand, DeleteCommand

Answer: a. SelectCommand, InsertCommand, UpdateCommand, DeleteCommand

What is the primary purpose of DataAdapter in ADO.NET?
a. To facilitate communication between a DataSet and a database
b. To execute SQL commands
c. To manage database connections
d. To create and manage database schema

Answer: a. To facilitate communication between a DataSet and a database

Which method of SqlDataReader is used to read the next record from the data stream?
a. Read()
b. Next()
c. MoveNext()
d. Fetch()

Answer: a. Read()

How do you set the command timeout for a SqlCommand object?
a. By setting the CommandTimeout property
b. By setting the Timeout property
c. By setting the ConnectionTimeout property
d. By using the SetTimeout() method

Answer: a. By setting the CommandTimeout property

Which method of SqlConnection is used to check if the connection is open?
a. State property
b. IsOpen() method
c. CheckStatus() method
d. GetState() method

Answer: a. State property

How do you execute a stored procedure that returns multiple result sets?
a. By using SqlDataReader and calling Read() multiple times
b. By using ExecuteNonQuery() method
c. By using ExecuteScalar() method
d. By using Fill() method

Answer: a. By using SqlDataReader and calling Read() multiple times

Which method of SqlCommand is used to execute a command that does not return any result sets?
a. ExecuteNonQuery()
b. ExecuteScalar()
c. ExecuteReader()
d. ExecuteDataSet()

Answer: a. ExecuteNonQuery()

What is the default behavior of SqlConnection regarding connection pooling?
a. It uses connection pooling by default to improve performance
b. It does not use connection pooling
c. It only pools connections if explicitly configured
d. It creates a new connection for each request

Answer: a. It uses connection pooling by default to improve performance

How do you handle exceptions when working with database connections in ADO.NET?
a. By using try-catch blocks
b. By using a finally block
c. By using if-else statements
d. By using using statements

Answer: a. By using try-catch blocks

Which method of SqlDataReader retrieves the value of a column as an object?
a. GetValue()
b. ReadValue()
c. FetchValue()
d. Retrieve()

Answer: a. GetValue()

What is the function of DataSet in ADO.NET?
a. To hold in-memory data from multiple tables
b. To manage database connections
c. To execute SQL commands
d. To handle data binding

Answer: a. To hold in-memory data from multiple tables

How do you create a new instance of SqlConnection in ADO.NET?
a. By using the new keyword and passing a connection string to the constructor
b. By calling the Create() method of SqlConnection
c. By using the Instantiate() method
d. By using the Open() method

Answer: a. By using the new keyword and passing a connection string to the constructor

Which property of SqlCommand specifies whether the command is for a stored procedure?
a. CommandType
b. CommandText
c. CommandProcedure
d. CommandCommand

Answer: a. CommandType

What is the purpose of SqlTransaction in ADO.NET?
a. To ensure that a series of database operations are executed as a single unit of work
b. To create and manage database connections
c. To execute SQL commands and queries
d. To fill a DataSet with data

Answer: a. To ensure that a series of database operations are executed as a single unit of work