Wednesday, 30 March 2011

DataReader Interview Questions

Can dataReader hold data from multiple tables?

Yes, DataReader can hold data from multiple tables.

 SqlConnection con = new SqlConnection("server=sugandha;initial catalog = dotnet;uid=sa;pwd=sugandha");
        string query = "select * from Employee; select * from Employee1";
        SqlCommand cmd = new SqlCommand(query, con);

       SqlDataReader dr = cmd.ExecuteReader();

        if (dr.HasRows)


            GridView1.DataSource = dr;


            if (dr.NextResult())

                GridView2.DataSource = dr;





How can we retrieve two tables of data at a time by using DataReader?

If we pass two queries in the commandText then we get data from two tables.
It return 2 tables in DataReader.

like :
string str="Select * from a;select * from b";

What is difference between dataset and datareader ?

Following are some major differences between dataset and datareader :

1. DataReader provides forward-only and read-only access to data , while the DataSet object can hold more than one table   from the same data source as well as the relationships between them.
2. Dataset is a disconnected architecture while datareader is connected architecture.
3. Dataset can persists contents while datareader can not persist contents , they are forward only.

I want to force the DataReader to return only schema of the datastore rather than data ?

objDataReader = objCommand.ExecuteReader(CommandBehavior.SchemaOnly)

How can we force the connection object to close after my DataReader is closed ?

ExecuteReader takes a parameter called as CommandBehavior where in we can specify saying close connection automatically after the DataReader is close.
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

DataSet Interview Questions

What is a DataSet ?

A DataSet is an in memory representation of data loaded from any data source.

How can we load multiple tables in to Dataset?

DataSet ds=new DataSet();
SqlDataAdapter dap=new SqlDataAdapter(Select * from <tablename>,<connection1>);
SqlDataAdapter dap1=new SqlDataAdapter(Select * from <tablename>,<connection1>);

How do we update a Dataset in ADO.Net and How do we update database through Dataset?

a. Update a DataSet:

DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(Query,connection);
Again we can add/update Dataset as below
SqlDataAdapter adp1 = new SqlDataAdapter(Query1,connection);

b. Update database through DataSet:

SqlCommandBuilder cmd = new SqlCommandBuilder(adp);
foreach(DataRow dr in ds.Table[0].Rows)
dr[“column Name”] = “value”;

What we do with the object of dataset after using it?Can we dispose it or can we set it nothing?Is it must or not?

we use dispose

If a table contains 20000 records . In a page at each time 100 records to be displayed.
What are the steps u will take to improve performance? will you use dataset or datareader?

We should go for DataSet as DataReader doesnot allow navigation between data. Suppoose we are seeing 1500 data and we want to see previous data in DataReader it is not possible but with DataSet we can do this.

what is typed and untyped dataset?
Typed DataSet has a schema and an Untyped DataSet does not have one.
It should be noted that the Typed Datasets have more support in Visual studio.

What is a DataTable?

A DataTable is a class in .NET Framework and in simple words a DataTable object represents a table from a database.

How do you merge two datasets into the third dataset in a simple manner?

Use DataSet1.Merge (DataSet2) method to merge two datasets and copy DataSet1 to DataSet3.

How to generate XML from a dataset and vice versa?

We can use WriteXml() and ReadXml() methods of DataSet Object.

What is Dataset object?

The DataSet provides the basis for disconnected storage and manipulation of relational data. We fill it from a data store, work with it while disconnected from that data store, then reconnect and flush changes back to the data store if required.

How can we save all data from dataset ?

Dataset has “AcceptChanges” method which commits all the changes since last time “Acceptchanges” has been executed.

How can we check that some changes have been made to dataset since it was loaded ?
Twist :- How can we cancel all changes done in dataset ? , How do we get values which are changed in a dataset ?

For tracking down changes Dataset has two methods which comes as rescue “GetChanges “and “HasChanges”.

GetChanges Return’s dataset which are changed since it was loaded or since Acceptchanges was executed.
HasChanges This property indicate’s has any change’s been made since the dataset was loaded or
acceptchanges method was executed.

 If  we want to revert or abandon all change’s since the dataset was loaded use “RejectChanges”.

How can we add/remove row’s in “DataTable” object of “DataSet” ?

”DataTable” has “DataRowCollection” object which has all rows in a “DataTable” object.
Following are the methods provided by “DataRowCollection” object :-

Add: Add’s a new row in DataTable
Remove: Remove’s a “DataRow” object from “DataTable”
RemoveAt: Remove’s a “DataRow” object from “DataTable” depending on index position of  the “DataTable”.

What’s difference between DataSet.Clone and DataSet.Copy ?

Clone: - It only copies structure, does not copy data. Copy: - Copies both structure and data.