Database Questions with Answers

8 practice questions on Database (C#) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Medium

Is the following true about a DataSet?
I Data TableCollectio object containing null or multiple Data Table objects(Columns, Rows, Constraints).
II. DataRelationCollection Object containing null or multiple DataRelation objects which establish a parent/child relation between two DataTable objects.

  1. I
  2. II
  3. I and II
  4. None of the above

Answer: C. I and II

Explanation

Both statements are true. A DataSet holds a DataTableCollection of its tables and a DataRelationCollection of relations between a parent and a child table, so the answer is I and II.

Step-by-step solution

  1. Statement I: the Tables property is a DataTableCollection holding DataTable objects.
  2. Statement II: the Relations property is a DataRelationCollection of DataRelation objects linking parent and child tables.
  3. Both statements are true, so the correct answer I and II is correct.

Why the other options are wrong

  • A. Statement II is also true, so I alone is incomplete.
  • B. Statement I is also true, so II alone is incomplete.
  • D. Both statements are true, so None of the above is wrong.

Tip: Check each statement on its own; if both hold, pick the both-true option.

Question 2Medium

How to get just first row, first column as an output using ADO.NET?

  1. ExecuteNonQuery()
  2. ExecuteReader()
  3. Executescalar()
  4. None of the above

Answer: C. Executescalar()

Explanation

Executescalar executes the query ,and return the column of the row first in the result set returned by the query. Additional or rows are ignored.

Step-by-step solution

  1. ExecuteNonQuery returns the number of affected rows, not data.
  2. ExecuteReader gives a reader over all the rows, so you still need to step through it.
  3. ExecuteScalar returns the first column of the first row and ignores the rest.
  4. So the correct answer, ExecuteScalar, is correct.

Why the other options are wrong

  • A. ExecuteNonQuery returns a count of affected rows, not the first row.
  • B. ExecuteReader returns all rows through a reader, not just the first value.
  • D. ExecuteScalar does exactly this, so None of the above is wrong.

Tip: ExecuteScalar gives one value; ExecuteNonQuery gives a row count for insert, update and delete.

Question 3Medium

Is the following true about connected and disconnected approach in ADO.NET?DataReader is connected approach and dataset is disconnected approach?

  1. Yes
  2. No

Answer: A. Yes

Explanation

Yes. A DataReader is connected: the connection must stay open while it reads. A DataSet is disconnected: the data is fetched, the connection can be closed, and the data is still available in memory.

Step-by-step solution

  1. A DataReader stays connected to the database while you read rows from it.
  2. A DataSet holds a copy of the data in memory and does not need the connection to stay open.
  3. So the statement is true, option A, Yes.

Why the other options are wrong

  • B. DataReader is the connected approach and DataSet is the disconnected approach, so No is wrong.

Tip: Connected means the connection stays open while reading (DataReader); disconnected means fetch, close, then work with the data (DataSet).

Question 4Medium

Is it good practice to call commands like insert, delete and update using Data Adapter?

SqlCommand sqlCommand = new SqlCommand("insert into tab values ('MyName', 35)", sqlConnection);
SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommand);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
  1. Yes
  2. No

Answer: B. No

Explanation

Commands like insert, delete are executed by calling the
ExecuteNonQuery method of SqlCommand. Let us see how we can write a simple function that will execute these commands. These commands can be used by passing a query from the application or by invoking
Stored procedures. using System;

usingSystem.Data;
usingSystem.Data.SqlClient;
class Program
{
Static string sqlConnectionString =
using System;
usingSystem.Data;
usingSystem.Data.SqlClient;

class Program
{
static string sqlConnectionString =@Server = .\SQLEXPRESS;
Database=Database;Trusted_Connection =True;;
static void Main(string[] args)
{
SqlConnectinsqlCommand = new SqlCommand(sqlConnectionString);
Try
{
SqlCommandsqlCommand = new SqlCommand(Insert into Tab
Select MyName, 35, sqlConnection);

sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
//Log the exception..send mail..etc
Console.WriteLine(Exception occurred :+ ex.Message);
}
Finally
{
If(sqlConnection.State! = ConnectionState.Closed)
sqlConnection.Close();
}
Console.ReadLine();
}
}

Step-by-step solution

  1. A data adapter is meant for fetching data and filling a dataset.
  2. Insert, delete and update through a plain command are better run with ExecuteNonQuery.
  3. Fill is designed for select queries.
  4. So option B (No) is correct.

Why the other options are wrong

  • A. Using Fill with insert statements misuses the adapter.

Tip: Use ExecuteNonQuery for insert, update and delete.

Question 5Medium

Is the following true about SQLConnection class? The SqlConnectio class handles database connections. It initiates a connection to your SQL database.The class is best used in a using resource acquisition statement. We call Open to query the database with SqlCommand.

  1. Yes
  2. No

Answer: A. Yes

Explanation

There are four basic steps while using ADO.NET
1 Connecting to the database
2 Passing the request to the database, i.e., a command like select, insert, or update (Open and close the connection before sending request and after receiveing results)
3 Getting back the results, ie., rows and / or the number of rows effected.
4 Storing the result and displaying it to the user The ADO.NET Connection Class is used to establish a connection to the database. The Connection class uses a ConnectionString to identify the database server location, authentication parameters, and other information to connect to the database.

Step-by-step solution

  1. SqlConnection handles database connections.
  2. It initiates a connection to SQL database.
  3. Using statement is best for resource management.
  4. Open method is called before querying with SqlCommand.
  5. All statements are true, so answer is Yes.

Why the other options are wrong

  • B. The statements about SqlConnection are all correct.

Question 6Medium

Which of the following is used to store data in memory?
I. DataSet
II. Data Table
III. SqlConnection
IV. DataAdapter

  1. I and II
  2. I and IV
  3. I, II and IV
  4. II and IV

Answer: A. I and II

Explanation

Data is read from a database. It is generated in memory from input. The DataTable/ DataSet type is a powerful way to store data .in memory. You may fetched this data from a database, or dynamically generated it.

Step-by-step solution

  1. DataSet and DataTable hold data in memory after it is read or generated.
  2. SqlConnection only opens a link to the database; it does not store data.
  3. DataAdapter moves data between the database and a DataSet but does not hold it itself.
  4. So the correct answer, I and II, is correct.

Why the other options are wrong

  • B. DataAdapter does not store data in memory.
  • C. SqlConnection and DataAdapter do not store data.
  • D. DataSet is a storage type, so leaving it out is wrong.

Tip: DataSet and DataTable store data; connections and adapters are the plumbing.

Question 7Medium

When a connection is closed, the connection is returned to the connection pool. True or false?

  1. True
  2. False

Answer: A. True

Explanation

If you close connection, it does not mean you disconnect from the database. It just returns the connection to the pool. If you open a connection, it means its simply a matter of obtaining an already open connection from the pool.
1 Open a connection when you need it, and
2 Close it as soon s you have finished with it.

Step-by-step solution

  1. Opening a connection takes one from the pool when a free one is available.
  2. Closing a connection does not drop the link to the database; it returns the connection to the pool for reuse.
  3. So the statement is True, option A.

Why the other options are wrong

  • B. Closing returns the connection to the pool, so the statement is true, not false.

Tip: Open a connection when you need it and close it as soon as you finish; the pool reuses it.

Question 8Medium

Which namespaces provide access to classes that represents the ADO.NET architecture?

  1. System.ADO
  2. System.DataStrcutures
  3. System.Data
  4. System.SQl

Answer: C. System.Data

Explanation

ADO.NET architecture. ADO.NET lets you build components that efficiently manage data from multiple data sources. In a disconnected scenario such as the Internet, ADO.NET provides the tools to request, update, and reconcile data in multiple tier systems. The ADO.NET architecture is also implemented in client applications, such as Windows Forms, or HTML pages created by ASP.NET. The centerpiece of the ADO.NET architecture is the DataSet class.

Each DataSet can contain multiple DataTable objects, with each DataTable containing data from a single data source, such as SQL Server. Each DataTable contains a DataColumnCollection--a collection of DataColumn objects--that determines the schema of each DataTable. The DataType property determines the type of data held by the DataColumn.

The ReadOnly and AllowDBNull properties let you further guarantee data integrity. The Expression property lets you construct calculated columns. If a DataTable participates in a parent/child relationship with another DataTable, the relationship is constructed by adding a DataRelation to the DataRelationCollection of a DataSet object. When such a relation is added, a UniqueConstraint and a ForeignKeyConstraint are both created automatically, depending on the parameter settings for the constructor. The UniqueConstraint guarantees that values that are contained in a column are unique.
The ForeignKeyConstraint determines what action will happen to the child row or column when a primary key value is changed or deleted.

Step-by-step solution

  1. ADO.NET uses System.Data namespace for data handling.
  2. System.Data contains classes like DataSet and DataTable.
  3. These classes manage data from different sources like SQL Server.
  4. Other options do not provide ADO.NET architecture classes.
  5. Correct answer is System.Data.

Why the other options are wrong

  • A. System.ADO is not a valid namespace in .NET.
  • B. System.DataStrcutures is misspelled and not used for ADO.NET.
  • D. System.SQl is not the correct namespace for ADO.NET classes.

Tip: Remember that ADO.NET architecture is built using System.Data namespace.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C# topics · All topics