Top 44 Ado.net Interview Questions You Must Prepare 28.Mar.2024

ADO.NET is a part of the Microsoft .NET Framework. This framework provides the set of classes that deal with data communication between various layers of the software architecture and the database. It provides a continuous access to different data source types such as SQL Server versions 7, 2000, 20@It also provides connectivity options to data sources through OLE DB and XML. Connectivity may be established with other databases like Oracle, MySQL etc. as well.
ADO.NET has the ability to separate data access mechanisms, data manipulation mechanisms and data connectivity mechanisms.
ADO.NET introduces along with it the disconnected architecture. In a disconnected architecture, data may be stored in a DataSet. It contains providers for connecting to databases, commands for execution and retrieval of results.
The classes for ADO.NET are stored in the DLL System.Data.dll.

Language Integrated Query or LINQ provides programmers and testers to query data and it uses strongly type’s queries and results.

ADO.NET maintains a disconnected database access model, which me, the application never remains connected constantly to the data source. Any changes and operations done on the data are saved in a local copy (dataset) that acts as a data source. Whenever, the connection to the server is re-established, these changes are sent back to the server, in which these changes are saved in the actual database or data source.

Following are uses of Stored Procedure:

  • Improved Performance.
  • Easy to use and maintain.
  • Security.
  • Less time and effort taken to execute.
  • Less Network traffic.

The DataSet object provides the following two methods to track down the changes: 
The GetChanges() method - Returns the DataSet object, which is changed since it was loaded or since the AcceptChanges() method was executed.
The HasChanges() method - Indicates if any changes occurred since the DataSet object was loaded or after a call to the AcceptChanges() method was made.
If you want to revert all changes since the DataSet object was loaded, use the RejectChanges() method.

Typed datasets use explicit names and data types for their members but untyped dataset uses table and columns for their members.

The DataRowCollection class defines the collection of rows for the DataTable object in a DataSet. The DataTable class provides the NewRow() method to add a new DataRow to DataTable. The NewRow method creates a new row, which implements the same schema as applied to the DataTable. The following are the methods provided by the DataRowCollection object:
Add() - Adds a new row to DataRowCollection.
Remove()- Removes a DataRow object from DataRowCollection.
RemoveAt() - Removes a row whose location is specified by an index number.

The prerequisites for connection pooling are as follows:

  • There must be multiple processes to share the same connection describing the same parameters and security settings.
  • The connection string must be identical.

A Partial class is a class that can be split into two or more classes. This me that a class can be physically separated into other parts of the class within the same namespace. All the parts must use the partial keyword. All the other classes should also have the same access modifier. At the compile time, all the partial classes will be treated as a single class. Let us list some advantages of having partial classes.

Thread.Abort() function stops the thread execution at any time.

There are two classes involved in System.Data.Common Nameapce:

  • DataColumnMapping.
  • DataTableMapping.

There are three different layers of ADO.Net:

  • Presentation Layer
  • Business Logic Layer
  • Database Access Layer

User-defined view of a table is contained in a DataView. A complete table or a small section of table depending on some criteria can be presented by an object of the DataView class. You can use this class to sort and find data within DataTable.
The DataView class has the following methods:
Find() - Finds a row in a DataView by using sort key value.
FindRows() - Uses the sort key value to match it with the columns of DataRowView objects. It returns an array of all the corresponding objects of DataRowView whose columns match with the sort key value.
AddNew() - Adds a new row to the DataView object.
Delete() - Deletes the specified row from the DataView object according to the specified index.

You must set the DataSource property and call the DataBind() method.Mention the namespace that is used to include .NET Data Provider for SQL server in .NET code.The System.Data.SqlClient namespace.

The DataAdapter class retrieves data from the database, stores data in a dataset, and reflects the changes made in the dataset to the database. The DataAdapter class acts as an intermediary for all the communication between the database and the DataSet object. The DataAdapter Class is used to fill a DataTable or DataSet Object with data from the database using the Fill() method. The DataAdapter class applies the changes made in dataset to the database by calling the Update() method. 
The DataAdapter class provides four properties that represent the database command: 
SelectCommand, InsertCommand, DeleteCommand, and UpdateCommand.

Following are the classes that are available in System.Data Namespace:

  • Dataset.
  • DataTable.
  • DataColumn.
  • DataRow.
  • DataRelation.
  • Constraint.

Following are the components of ADO.Net Data provider:
Connection object – Represents connection to the Database
Command object – Used to execute stored procedure and command on Database
ExecuteNonQuery – Executes command but doesn’t return any value
ExecuteScalar – Executes and returns single value
ExecuteReader – Executes and returns result set
DataReader – Forward and read only recordset
DataAdapter – This acts as a bridge between database and a dataset.

The various methods provided by the DataSet object to generate XML are:

  • ReadXml() - Reads XML document into a DataSet object.
  • GetXml() - Returns a string containing an XML document.
  • WriteXml() - Writes an XML data to disk.

AD0.NET consists of two fundamental components:

  • The DataSet, which is disconnected from the data source and does not need to know where the data that it holds is retrieved from.
  • The .net data provider, which allows you to connect your application to the data source and execute the SQL commands against it.

The data provider contains the Connection, Command, DataReader, and DataAdapter objects.The Connection object provides connectivity to the database. The Command object provides access to database commands to retrieve and manipulate data in a database. The DataReader object retrieves data from the database in the readonly and forward-only mode. The DataAdapter object uses Command objects to execute SQL commands. The DataAdapter object loads the DataSet object with data and also updates changes that you have made to the data in the DataSet object back to the database.

With respect to usage, there is no difference between OLEDB Provider and SqlClient. The difference lies in their performance. SqlClient is explicitly used to connect your application to SQL server directly, OLEDB Provider is generic for various databases, such as Oracle and Access including SQL Server.Therefore, there will be an overhead which leads to performance degradation.

The DataReader object helps in retrieving the data from a database in a forward-only, read-only mode. The base class for all the DataReader objects is the DbDataReader class.
The DataReader object is returned as a result of calling the ExecuteReader() method of the Command object. The DataReader object enables faster retrieval of data from databases and enhances the performance of .NET applications by providing rapid data access speed. However, it is less preferred as compared to the DataAdapter object because the DataReader object needs an Open connection till it completes reading all the rows of the specified table. 
An Open connection to read data from large tables consumes most of the system resources. When multiple client applications simultaneously access a database by using the DataReader object, the performance of data retrieval and other related processes is substantially reduced. In such a case, the database might refuse connections to other .NET applications until other clients free the resources.

Sort() method of GridViewControl is used to sort the data in a datatable.

There are different methods under SqlCommand and they are:
Cancel – Cancel the query
CreateParameter – returns SQL Parameter
ExecuteNonQuery – Executes and does not return result set
ExecuteReader – executes and returns data in DataReader
ExecuteScalar – Executes and returns single value
ExecuteXmlReader – Executes and return data in XMLDataReader object
ResetCommandTimeout – Reset Timeout property

The following are the usages of the Command object in AD0.NET:
The Command object in AD0.NET executes a command against the database and retrieves a DataReader or DataSet object.

  • It also executes the INSERT, UPDATE, or DELETE command against the database.
  • All the command objects are derived from the DbCommand class.
  • The command object is represented by two classes: SqlCommand and OleDbCommand.
  • The Command object provides three methods to execute commands on the database:
    • The ExecuteNonQuery() method executes a Tract-SQL statement against the connection and returns the number of rows affected.
    • The ExecuteScalar() method returns a single value from a database query.
    • The ExecuteReader() method returns a result set by using the DataReader object.

Yes, it is possible to load multiple tables in a single dataset.

DataSet ds=new DataSet();
SqlDataAdapter dap=new SqlDataAdapter(Select * from ,);
dap.Fill(ds,”TableOne”);
SqlDataAdapter dap1=new SqlDataAdapter(Select * from ,);
dap1.Fill(ds,”tableTwo”);

The DataSource property and the DataMember property are used to bind a DataGridView control.

Object pooling is a concept of storing a pool (group) of objects in memory that can be reused later as needed. Whenever, a new object is required to create, an object from the pool can be allocated for this request; thereby, minimizing the object creation. A pool can also refer to a group of connections and threads. Pooling, therefore, helps in minimizing the use of system resources, improves system scalability, and performance.

There are various methods of XML dataset object:
GetXml() – Get XML data in a Dataset as a single string.
GetXmlSchema() – Get XSD Schema in a Dataset as a single string.
ReadXml() – Reads XML data from a file.
ReadXmlSchema() – Reads XML schema from a file.
WriteXml() – Writes the contents of Dataset to a file.
WriteXmlSchema() – Writes XSD Schema into a file.

The parameters that control most of connection pooling behaviors are as follows:

  • Connect Timeout
  • Max Pool Size
  • Min Pool Size
  • Pooling

Dataset.clone object copies structure of the dataset including schemas, relations and constraints. This will not copy data in the table.
Dataset.copy – Copies both structure and data from the table.

ExecuteNonQuery is the best method to get two values from the database.

Yes, stored procedures are used in ADO.Net and it can be used for common repetitive functions.

The default timeout of Sqlcommand. CommandTimeout property is 30 Seconds.

Following are the Data Providers used in ADO.Net:.

  • MS SQL Server.
  • OLEDB.
  • ODBC.

SQL Server should authenticate before performing any activity in the database. There are two types of authentication:
Windows Authentication – Use authentication using Windows domain accounts only.
SQL Server and Windows Authentication Mode – Authentication provided with the combination of both Windows and SQL Server Authentication.

DataAdapter is used to retrieve data from a data source .Insertcommand, UpdateCommand and DeleteCommand are the commands object used in DataAdapter to manage update on the database.

OLEDB provider is used to access any database and provides flexibility of changing the database at any time. SQLClient provider is used to access only SQL Server database but it provides excellent performance than OLEDB provider while connecting with SQL Server database.

One of the major component of ADO.NET is the DataSet object, which always remains disconnected from the database and reduces the load on the database.

Command is used to execute all kind of queries like DML and DDL. DML is nothing but Insert, Update and Delete. DDL are like Create and drop tables.
Command Builder object is used to build and execute DML queries like Create and Drop Tables.

Connection pooling refers to the task of grouping database connections in cache to make them reusable because opening new connections every time to a database is a time-consuming process. Therefore, connection pooling enables you to reuse already existing and active database connections, whenever required, and increasing the performance of your application.
You can enable or disable connection pooling in your application by setting the pooling property to either true or false in connection string. By default, it is enabled in an application.

DataRelation object is used to add relationship between two or more datatable objects.

Following are different execute methods of ADO.Net command object:
ExecuteScalar – Returns single value from the dataset
ExecutenonQuery – Returns resultset from dataset and it has multiple values
ExecuteReader – Forwardonly resultset
ExecuteXMLReader – Build XMLReader object from a SQL Query

The Clone() method copies only the structure of a DataSet. The copied structure includes all the relation, constraint, and DataTable schemas used by the DataSet. The Clone() method does not copy the data, which is stored in the DataSet. 
The Copy() method copies the structure as well as the data stored in the DataSet.