ADO.NET Interview Questions and Answers SET – 1



Following are some basic Interview Questions and Answers of ADO.NET.

What is the difference between ExecuteQuery() and ExecuteNonQuery()?
  • ExecuteQuery is used for select query whereas ExecuteNonQuery is used for insert, update and delete queries
  • ExecuteQuery returns result set whereas ExecuteNonQuery returns integer value(number of rows affected)
What is a dataset?

Dataset is the in-memory cache of data. The DataSet contains a collection of one or more DataTable objects made up of rows and columns of data just like table in the database.

What is the difference between dataset and datareader?

Dataset

  1. Disconnected architecture
  2. No need to manually open and close the connection
  3. Data in dataset is inspected only when the whole dataset is filled.
  4. DataSet represents a complete set of data including related tables, constraints, and relationships among the tables.
  5. Dataset is useful when you want to serialize the results.

Datareader

  1. Connected architecture
  2. Connection is manually open and close
  3. Datareader is a good option when retrieving the large amount of data
  4. The ADO.NET DataReader is used to retrieve read-only, forward-only data from a database
  5. Datareader stores only one result at a time so it increases application performance and reduces system overheads.
Can we sort data from a SQLDataSource when mode is set to DataReader?

No. A DataReader is a read only object. It is a quick way for reading through sequential data but you cannot go backwards through it.

Consider an example

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<your_connection_string>" 
	      DataSourceMode="DataReader"
        SelectCommand="SELECT * FROM [Subjects]">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowSorting="true">
</asp:GridView>

Here grid view is bound using SqlDataSource control. When you run the program you can see the gridview with data in it. When you try to sort the records you will get following error.

The SqlDataSource ‘SqlDataSource1’ cannot sort. Set DataSourceMode to DataSet to enable sorting. If you are using a stored procedure that supports sorting, set the SortParameterName property to the name of the stored procedure’s parameter that accepts a sort expression.

If you want to give sorting, paging and filtering like capabilities then change the DataSourceMode from DataReader to DataSet.

How to merge 2 datatables?

if table1 and table2 are the instances of DataTable class then you can merge table2 in table1 using Merge method

table1.Merge(table2);