Saturday, 15 March 2014

Interview Questions And Answers on ADO.NET in C#(part 2)

Q. 1 What are major difference between classic ADO and ADO.NET ?

A.    Following are some major differences between both Ado and Ado.Net

? As in classic ADO we had client and server side cursors they are no more present in ADO.NET. Note it's a disconnected model so they are no more applicable.
? Locking is not supported due to disconnected model.
? All data persist in XML as compared to classic ADO where data persisted in Binary format also.

Q.2 What is  ADO.NET?

A. ADO.Net is the part of the Microsoft .Net Framwork.  This framwork provides the set of classes  that deals with a data communication between various layers of the Software Architecture and the database.

Q.3 DataAdapter Object populates a DataSet and resolves updates with the DataSource. Yes/no?

A.   yes.

 Q.4 What is the role of data provider?

  A. The .NET data provider layer resides between the application and the database. Its task is to take care of all their   interactions.

The .NET Data provider can be demonstrated to be:

SQL Server data provider 
OLEDB data provider 
ODBC Data Provider

ADO.NET supports the following OLE DB Providers:

-SQLOLEDB - Microsoft OLE DB Provider for SQL Server.
- MSDAORA - Microsoft OLE DB Provider for Oracle.
- Microsoft.Jet.OLEDB.4.0 - OLE DB Provider for Microsoft Jet. 

Q 5.  How does object pooling and connection pooling differ?

A.  In Object pooling, you can control the number of connections.
In connection pooling, you can control the maximum number reached.
When using connection pooling, if there is nothing in the pool, a connection is created since the creation is on the same  thread.

In object pooling, the pool decides the creation of an object depending on whether the maximum is reached which in case if it is, the next available object is returned. However, this could increase the time complexity if the object is heavy.

Q.6  what is the NameSpace in which .Net has the data functionality    classes?

A. Following are the NameSpaces provided by .NET for data management :-

system.Data;
system.data.oledb;
system.data.sqlclient;
system.xml; etc....

   Q.7 Can you give an overview of ADO.NET architecture?

A.  The most important section in ADO.NET architecture is “Data Provider”. Data Provider provides access to datasource (SQL SERVER, ACCESS, ORACLE).In short it provides object to achieve functionalities like opening and closing connection, retrieve data and update data. In the below figure you can see the four main sections of a data provider :- 

? Connection.
? Command object (This is the responsible object to use stored procedures) .
? Data Adapter (This object acts as a bridge between datastore and dataset).
? Datareader (This object reads data from data store in forward only mode).

Q.8  What are the two fundamental objects in ADO.NET ?

A.   Datareader and Dataset are the two fundamental objects in ADO.NET.

 Q.9 What is difference between dataset and datareader ?

A.   Following are some major differences between dataset and datareader :-

? DataReader provides forward-only and read-only access to data, while the DataSet object can hold more than one table (in other words more than one rowset) from the same data source as well as the relationships between them.
? Dataset is a disconnected architecture while datareader is connected architecture.
? Dataset can persist contents while datareader can not persist contents, they are forward only.

Q.10  What is the use of command objects and what are the methods provided by the command object ?

A. They are used to connect connection object to Datareader or dataset.
Following are the methods provided by command object :-

ExecuteScaller();
ExecuteNonquery();
ExecuteReader();

Q.11 How can we force the connection object to close after my datareader is closed ?

A. Command method Executereader takes a parameter called as CommandBehavior where in we can specify      saying close connection automatically after the Datareader is close.

 pobjDataReader = pobjCommand.ExecuteReader(CommandBehavior.CloseConnection)

Q.12   I want to force the datareader to return only schema of the datastore rather than data ?

A. pobjDataReader = pobjCommand.ExecuteReader(CommandBehavior.SchemaOnly)

 Q.13  How can we fine tune the command object when we are expecting a single row or a single value ?

A. Again CommandBehaviour enumeration provides two values SingleResult and SingleRow. If you are expecting a single value then pass “CommandBehaviour.SingleResult” and the query is optimized accordingly, if you are expecting single row then pass “CommandBehaviour.SingleRow” and query is optimized according to single row.

 Q.14  Which is the best place to store connectionstring in .NET project?

A.Config files are the best places to store connectionstrings.

 If it is a web-based application “Web.config” file will be used and if it is a windows application “App.config” files will be used.

Q.15 What is the use of dataadapter?

A.These are objects that connect one or more Command objects to a Dataset object. They provide logic that would get data from the data store and populates the tables in the DataSet, or pushes the changes in the DataSet back into the data store.

 An OleDbDataAdapter object is used with an OLE-DB provider.
 A SqlDataAdapter object uses Tabular Data Services with MS SQL Server.

There are three most commonly used methods of Dataadapter :-

Fill();
updata();
FillSchema();

Q.16  How can we save all data from dataset ?

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

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

A.“Datatable” provides “NewRow” method to add new row to “DataTable”.
“DataTable” has “DataRowCollection” object which has all rows in a “DataTable” object.
Following are the methods provided by “DataRowCollection” object :-

add();
remove();
removeat();

Q.18  What is basic use of “DataView” ?

A.“DataView” represents a complete table or can be small section of rows depending on some criteria. It is best used for sorting and finding data with in “datatable”. Dataview has the following method’s :- 

Find();
Findrow();
addnew();
delete();

 Q.19  What is the use of CommandBuilder ?

A.CommandBuilder builds “Parameter” objects automatically. Below is a simple code which uses commandbuilder to load its parameter objects.

Dim pobjCommandBuilder As New OleDbCommandBuilder(pobjDataAdapter)           pobjCommandBuilder.DeriveParameters(pobjCommand)

Be careful while using “DeriveParameters” method as it needs an extra trip to the Datastore which can be very inefficient.

 Q.20 What’s difference between “Optimistic” and “Pessimistic” locking?

A.    In pessimistic locking when user wants to update data it locks the record and till then no one can update data. Other user’s can only view the data when there is pessimistic locking.

         In optimistic locking multiple users can open the same record for updating, thus increase maximum concurrency. Record is only locked when updating the record. This is the most 299 preferred way of locking practically. Now a days browser based application is very common and having pessimistic locking is not a practical solution.

Q.21 How many ways are there to implement locking in ADO.NET ?

A. Following are the ways to implement locking using ADO.NET :-

1) When we call “Update” method of DataAdapter it handles locking internally .
2) Define a Datetime stamp field in the table.When actually you are firing the UPDATE SQL statements compare the current timestamp with one existing in the database.
3) Define a Datetime stamp field in the table.When actually you are firing the UPDATE SQL statements compare the current timestamp with one existing in the database.

  Q.22  What is difference between Dataset. clone and Dataset. copy ?

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

 Q.23 Can you explain the difference between an ADO.NET Dataset and an ADO Recordset?

A. There two main basic differences between recordset and dataset :-

1) With dataset you can retrieve data from two databases like oracle and sql server and merge them in one dataset , with recordset this is not possible.
2)  All representation of Dataset is using XML while recordset uses COM.
3)  Recordset can not be transmitted on HTTP while Dataset can be
.
Q. 24    What is Maximum Pool Size in ADO.NET Connection String?

A.     Maximum pool size decides the maximum number of connection objects to be pooled. If the maximum     pool size is reached and there is no usable connection available the request is queued until connections are released back in to pool. So it’s always a good habit to either call the close or dispose method of the connection as soon as you have finished work with the connection object.

Q.25  How to enable and disable connection pooling?

A.  For .NET it is enabled by default but if you want to just make sure set Pooling=true in the connection string. To disable connection pooling set Pooling=false in connection string if it is an ADO.NET Connection. If it is an OLEDBConnection object set OLE DB Services=-4 in the connection string.

Q.26 Explain the three services model?

A.Three-tier application:-
       1) . Presentation (UI),
       2)  business (logic and underlying code) and
       3) data (from storage or other sources). 

Q. 27 What does the parameter Initial Catalog define inside Connection String?

A.The database name to connect to. 

Q.28  What's the data provider name to connect to Access database?

A. Microsoft.Access. 

Q.29 What does Dispose method do with the connection object?

A.Deletes it from the memory. 

Q. 30 What is a pre-requisite for connection pooling?

A.  Multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings.

Q.31 which of the following is the method provided by the DataSet Object to generate XML?

A.   ReadXML,WritenXML and GetXML.

Q.32 when you create a relation between tables in ADO.Net  DataSet?

A. 1) a UniqueConstraint is added to the parent table ,
     2)  a ForeignKeyConstraint is added to the child table by default .

Q.33How to run a stored procedure from .Net code?

A. use the command object and it's ExecuteNonQuery method to run a DDL  or DDL statements of SQL. First we create connection object an command object and configure these objects for the statement you wish to exicute . Open the database connection.call ExecuteNonQuery  on the command. Close the connection.

Q.34 what is the SelectCommandBuilder in ADO. Net?

A. SelectCommandBuilder is class  in ADO.Net provides the feature of reflecting the changes made to a dataset or  an instance  of the sql server data. The scb object automatically genarates the values contained with in the sqldataadapter's Insertcommand, updatecommand and deletecommand properties based on the  initial selectcommand. The advantage is here is that will  not need to write selectcommand and  sqlparameter stype  explicitly.

Q.35 which method of the command object is best suited  when you have aggregate functions in a select statements?

A. ExecuteScalar.

Q.36.
How do I delete a row from a DataTable?


ds.Tables("data_table_name").Rows(i).Delete
dscmd.update(ds,"data_table_name")

Q.37. What is diffgram?

a.It is a format which dataset use internally while come with xml files.



No comments:

Post a Comment