ASP.NET Interview Question Categories
1)What is the namespace in which .NET has the data functionality classes ?
Following are the namespaces provided by .NET for data management :-
->System.data
This contains the basic objects used for accessing and storing relational data, such as DataSet,DataTable, and DataRelation. Each of these is independent of the type of data source and the way we connect to it.
->System.Data.OleDB
It contains the objects that we use to connect to a data source via an OLE-DB provider, such as OleDbConnection, OleDbCommand, etc. These objects inherit from the common base classes, and so have the same properties, methods, and events as the SqlClient equivalents.
->System.Data.SqlClient:
This contains the objects that we use to connect to a data source via the Tabular Data Stream (TDS) interface of Microsoft SQL Server (only). This can generally provide better performance as it removes some of the intermediate layers required by an OLE-DB connection.
->System.XML
This Contains the basic objects required to create, read, store, write, and manipulate XML documents according to W3C recommendations.
2)Can you give a overview of ADO.NET architecture ?
Ans) 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).
Dataset object represents disconnected and cached data. If you see the diagram it is not in direct connection with the data store (SQL SERVER, ORACLE etc) rather it talks
with Data adapter, who is responsible for filling the dataset. Dataset can have one or more Datatable and relations.
3)What are the two fundamental objects in ADO.NET ?
Ans) Datareader and Dataset are the two fundamental objects in ADO.NET.
(4)What is difference between dataset and datareader ?
Ans)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.
5)What is the use of connection object ?
Ans) They are used to connect a data to a Command object.
√ An OleDbConnection object is used with an OLE-DB provider
√ A SqlConnection object uses Tabular Data Services (TDS) with MS SQL Server
6)What is the use of command objects and what are the
methods provided by the command object ?
Ans) They are used to connect connection object to Datareader or dataset. Following are the methods provided by command object :-
√ ExecuteNonQuery :- Executes the command defined in the CommandText
property against the connection defined in the Connection property for a query
that does not return any row (an UPDATE, DELETE or INSERT). Returns
an Integer indicating the number of rows affected by the query.
√ ExecuteReader :- Executes the command defined in the CommandText property
against the connection defined in the Connection property. Returns a "reader"
object that is connected to the resulting rowset within the database, allowing
the rows to be retrieved.
√ ExecuteScalar :- Executes the command defined in the CommandText property
against the connection defined in the Connection property. Returns only
single value (effectively the first column of the first row of the resulting rowset)
any other returned columns and rows are discarded. It is fast and efficient
when only a "singleton" value is required.
7)What is the use of dataadapter ?
Ans)
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.
8)What are basic methods of Dataadapter ?
Ans)There are three most commonly used methods of Dataadapter :-
Fill :- Executes the SelectCommand to fill the DataSet object with data from the data
source. It an also be used to update (refresh) an existing table in a DataSet with changesmade to the data in the original datasource if there is a primary key in the table in the DataSet.
FillSchema :- Uses the SelectCommand to extract just the schema for a table from the
data source, and creates an empty table in the DataSet object with all the corresponding constraints.
Update:- Calls the respective InsertCommand, UpdateCommand, or DeleteCommand for
each inserted, updated,or deleted row in the DataSet so as to update the original data
source with the changes made to the content of the DataSet. This is a little like the
UpdateBatch method provided by the ADO Recordset object, but in the DataSet it can
be used to update more than one table.
(9)What is Dataset object?
Ans) 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.
(10)What are the various objects in Dataset ?
Ans) Dataset has a collection of DataTable object within the Tables collection. Each DataTable object contains a collection of DataRow objects and a collection of DataColumn objects. There are also collections for the primary keys, constraints, and default values used in this table which is called as constraint collection, and the parent and child relationships between the tables. Finally, there is a DefaultView object for each table. This is used to create a DataView object based on the table, so that the data can be searched, filtered or otherwise manipulated while displaying the data.
11)How can we force the connection object to close after my datareader is closed ?
Ans) 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)
12)I want to force the datareader to return only schema of the datastore rather than data ?
Ans) pobjDataReader = pobjCommand.ExecuteReader(CommandBehavior.SchemaOnly)
13)How can we fine tune the command object when we are expecting a single row or a single value ?
Ans) 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.
14)Which is the best place to store connectionstring in .NET projects ?
Ans) 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.
15)What are the various methods provided by the dataset object to generate XML?
Ans)
√ ReadXML
Read’s a XML document in to Dataset.
√ GetXML
This is a function which returns the string containing XML document.
√ WriteXML
This writes a XML data to disk
16)How can we save all data from dataset ?
Ans) Dataset has “AcceptChanges” method which commits all the changes since last time
“Acceptchanges” has been executed.
17)How can we check that some changes have been made to dataset since it was loaded ?
Ans) GetChanges
Returns dataset which are changed since it was loaded or since Acceptchanges was
executed.
HasChanges
This property indicates that has any changes been made since the dataset was loaded or acceptchanges method was executed.
If we want to revert or abandon all changes since the dataset was loaded use
“RejectChanges”.
18) How can we add/remove row’s in “DataTable” object of “DataSet” ?
Ans) “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
Adds a new row in DataTable
Remove
It removes a “DataRow” object from “DataTable”
RemoveAt
It removes a “DataRow” object from “DataTable” depending on index position of the
“DataTable”.
19)What is basic use of “DataView” ?
Ans) “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
It takes a array of values and returns the index of the row.
FindRow
This also takes array of values but returns a collection of “DataRow”.
If we want to manipulate data of “DataTable” object create “DataView” (Using the
“DefaultView” we can create “DataView” object) of the “DataTable” object and use the
following functionalities :-
AddNew
Adds a new row to the “DataView” object
Delete
Deletes the specified row from “DataView” object
20)What is the difference between “DataSet” and “DataReader” ?
Ans) Following are the major differences between “DataSet” and “DataReader” :-
√ “DataSet” is a disconnected architecture, while “DataReader” has live
connection while reading data. If we want to cache data and pass to a
different tier “DataSet” forms the best choice and it has decent XML support.
√ When application needs to access data from more than one table “DataSet”
forms the best choice.
√ If we need to move back while reading records, “datareader” does not support
this functionality.
√ But one of the biggest drawbacks of DataSet is speed. As “DataSet” carry
considerable overhead because of relations, multiple tables etc speed is slower
than “DataReader”. Always try to use “DataReader” wherever possible, as
it’s meant specially for speed performance.
21)How can we load multiple tables in a DataSet ?
Ans)
objCommand.CommandText = "Table1"
objDataAdapter.Fill(objDataSet, "Table1")
objCommand.CommandText = "Table2"
objDataAdapter.Fill(objDataSet, "Table2")
22)How can we add relation’s between table in a DataSet ?
Ans) Dim objRelation As DataRelation
objRelation=New
DataRelation("CustomerAddresses",objDataSet.Tables("Customer").Columns("Custid")
,objDataSet.Tables("Addresses").Columns("Custid_fk"))
objDataSet.Relations.Add(objRelation)
Relations can be added between “DataTable” objects using the “DataRelation” object.
Above sample code is trying to build a relationship between “Customer” and “Addresses”
“Datatable” using “CustomerAddresses” “DataRelation” object.
23)What is the use of CommandBuilder ?
Ans) 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
24)What is difference between Dataset. clone and Dataset. copy ?
Ans)Clone: - It only copies structure, does not copy data.
Copy: - Copies both structure and data
25)Can you explain the difference between an ADO.NET Dataset and an ADO Recordset?
Ans) There two main basic differences between recordset and dataset :-
√ With dataset you an retrieve data from two databases like oracle and sql
server and merge them in one dataset , with recordset this is not possible
√ All representation of Dataset is using XML while recordset uses COM.
√ Recordset can not be transmitted on HTTP while Dataset can be.
26)What is Maximum Pool Size in ADO.NET Connection String?
Ans)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.
(27)How to enable and disable connection pooling?
Ans) 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.
28)Why DataReader is Forward Only?
Ans) DataReaader is connected object and one can process the rows that are returned by query, one at a time. It discards every row after you have gone through it and so it is extremely fast.It contains only read-only data, so no updates are allowed using DataReader objects.
29)What is Microsoft ADO.NET?
Ans) Visual Studio .NET provides access to databases through the set of tools and namespaces collectively referred to as Microsoft ADO.NET
30)What are the 3 major types of connection objects in ADO.NET?
Ans) OleDbConnection object : Use an OleDbConnection object to connect to a Microsoft Access or third-party database, such as MySQL. OLE database connections use the OleDbDataAdapter object to perform commands and return data.
SqlConnection object : Use a SqlConnection object to connect to a Microsoft SQL Server database. SQL database connections use the SqlDataAdapter object to perform commands and return data.
OracleConnection object : Use an OracleConnection object to connect to Oracle databases. Oracle database connections use the OracleDataAdapter object to perform commands and return data. This connection object was introduced in Microsoft .NET Framework version 1.1.
31)List the 4 common ADO.NET Namespaces?
Ans) System.Data : Contains Classes, types, and services for creating and accessing data sets and their subordinate objects
System.Data.SqlClient : Contains Classes and types for accessing Microsoft SQL Server databases
System.Data.OracleClient : Contains Classes and types for accessing Oracle databases (Microsoft .NET Framework version 1.1 and later)
System.Data.OleDb : Contains Classes and types for accessing other databases
32)List all the steps in order, to access a database through ADO.NET?
1. Create a connection to the database using a connection object.
2. Invoke a command to create a DataSet object using an adapter object.
3. Use the DataSet object in code to display data or to change items in the database.
4. Invoke a command to update the database from the DataSet object using an adapter object.
5. Close the database connection if you explicitly opened it in step 2 using the Open method. Invoking commands without first invoking the Open method implicitly opens and closes the connection with each request.
33)Why will you usually create an ASPNET user account in the Database for an ASP.NET web application?
Ans) Web applications run using the ASPNET user account. The SQL database administrator will have to set up this account and grant it permissions before your Web application will have access to a SQL database. For file-based databases, such as Microsoft Access, you must grant permissions on the database file to the ASPNET user account using Windows file security settings.
34)What is the difference between DataReader and DataAdapter?
1. Data Reader is read only forward only and much faster than DataAdapter.
2. If you use DataReader you have to open and close connection explicitly where as if you use DataAdapter the connection is automatically opened and closed.
3. DataReader is connection oriented where as Data Adapter is disconnected
35)Can you inherit from SqlConnection Class?
Ans) No, you cannot inheirt from SqlConnection Class. SqlConnection Class is a sealed class. It is a compile time error.
36)Will the connection be closed, if the SqlConnection object goes out of scope?
Ans) No, If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose.
37)What happens if connection pooling is enabled?
Ans) If connection pooling is enabled and when you call Close or Dispose methods, then the connection is returned to the connection pool. This connection can then be resused.If connection pooling is disabled and when you call Close or Dispose methods, the underlying connection to the server is actually closed.
38)How do you ensure that the database connections are always closed?
Ans) To ensure that the database connections are always closed, open the connection inside of a using block, as shown in the following code fragment. Doing so ensures that the connection is automatically closed when the code exits the block.
using (SqlConnection ConnectionObject = new SqlConnection())
{
ConnectionObject.Open();
//The database connection will be closed when the control exits the using code block
}
38)How do you read an XML file into a DataSet?
Ans) Using the DataSet object’s ReadXML method.
39)When do you use ExecuteReader, ExecuteNonQuery, ExecuteScalar methods?
Ans)If the command or stored procedure that is being executed returns a set of rows, then we use ExecuteReader method.
If the command or stored procedure that is being executed returns a single value then we use ExecuteScalar method.
If the command or stored procedure performs INSERT, DELETE or UPDATE operations, then we use ExecuteNonQuery method. ExecuteNonQuery method returns an integer specifying the number of rows inserted, deleted or updated.
40)Can your class inherit from SqlCommand Class?
Ans)No, you cannot inheirt from SqlCommand Class. SqlCommand Class is a sealed class. It is a compile time error.
41)Give an example that shows how to execute a stored procedure in ADO.NET?
using (SqlConnection ConnectionObject = new SqlConnection())
{
//Specify the name of the stored procedure to execute and the Connection Object to use
SqlCommand CommandObject = new SqlCommand("StoredProcedureName", ConnectionObject);
//Specify the SQL Command type is a stored procedure
CommandObject.CommandType = CommandType.StoredProcedure;
//Open the connection
ConnectionObject.Open();
//Execute the Stored Procedure
int RecordsAffected = CommandObject.ExecuteNonQuery();
}
42)Can you reuse a SqlCommand object?
Yes, you can reset the CommandText property and reuse the SqlCommand object.
43)What are the methods that can ensure asynchronous execution of the Transact-SQL statement or stored procedure?
Ans) BeginExecuteNonQuery
BeginExecuteReader
44)What is SqlCommand.CommandTimeout Property used for?
Ans) CommandTimeout Property is used to Get or set the wait time before terminating the attempt to execute a command and generating an error.
//Specify the CommandTimeout property value
SqlCommand CommandObject = new SqlCommand("StoredProcedureName", ConnectionObject);
//Wait for 10 seconds to execute the Stored procedure
CommandObject.CommandTimeout = 10;
The time is in seconds. The default is 30 seconds.
45)what are the differences between ADO.net 1.0 and ADO.net 2.0?
Ans)1. Ado.net 2.0 provides MARS ( multiple active resultsets which allows you to have two datareader on one connection open at sametime.
2. Ado.net 2.0 provide bulk copy facility.
3. Ado.net 2.0 provides table adapters and hence allows easy to create DAL.
46)What are the Advantages of ADO.Net?
Ans)
1. ADO.NET Does Not Depend On Continuously Live Connections
2. Database Interactions Are Performed Using Data Commands
3. Data Can Be Cached in Datasets
4. Datasets Are Independent of Data Sources
5. Data Is Persisted as XML
6. Schemas Define Data Structures
47)What is difference in Record set and Dataset?
Recordset provides data one row at a time.
Dataset is a data structure which represents the complete table data at same time.
Recordset has the logic to update and manipulate data
Dataset is just a data store and manipulation is done through DataAdapters in .NET.
How to add auto increment column in the DataTable?
// create columns for the DataTable
DataTable dTable = new DataTable();
DataColumn auto = new DataColumn("AutoID", typeof(System.Int32));
dTable.Columns.Add(auto);
// specify it as auto increment field
auto.AutoIncrement = true;
auto.AutoIncrementSeed = 1;
auto.ReadOnly = true;
How to create a column in the DataTable?
DataTable dTable = new DataTable();
// create another column
DataColumn name = new DataColumn("Name", typeof(string));
dTable.Columns.Add(name);
How to add a new row in DataTable?
DataTable dTable = new DataTable();
DataRow row = null;
for (int i = 0; i < 5; i++)
{
row = dTable.NewRow ();
row["Name"] = i + " - Raja";
row["Address"] = "USA";
dTable.Rows.Add(row);
}
Difference between OLEDB Provider and SqlClient ?
SQLClient .NET classes are highly optimized for the .net / sqlserver combination and achieve optimal results. The SqlClient data provider is fast. It's faster than the Oracle provider, and faster than accessing database via the OleDb layer. It's faster because it accesses the native library (which automatically gives you better performance), and it was written with lots of help from the SQL Server team.
What are the advantage of ADO.Net?
• ADO.NET Does Not Depend On Continuously Live Connections
• Database Interactions Are Performed Using Data Commands
• Data Can Be Cached in Datasets
• Datasets Are Independent of Data Sources
• Data Is Persisted as XML
• Schemas Define Data Structures
Explain about Data access objects or DAO?
DAO is used for database access on windows platform. It creates a work space object in which applications or operations are performed. There are two types of database engines they are Jet database engine and ODBC direct database engine.
Explain about the relationship of XML and ADO.NET?
ADO.NET utilizes the power of XML by providing disconnected access to data. This is designed with the help of XML classes in .NET Framework which form the components of single architecture.
Name some of the top level objects which ADO consists?
1. Connection object is responsible for creating a connection to the database.
2. Record object represents data which is not from the database.
3. Parameter object represents a sql parameter
4. Stream object is responsible to represent data from a text page or web page.
How to create a DataView from DataTable?
In order to create a DataView from a DataTable, use instantiate the DataView object by passing DataTable as parameter in the constructor.
eg:DataView dView = new DataView(dTable);
Can DataAdapter object accept DataTable as parameter in Fill method?
Yes,
DataAdapter object can accept either DataTable or DataSet as parameter to fill data from database.
eg:
SqlDataAdapter dAd = new SqlDataAdapter();
DataTable dTable = new DataTable();
DataSet dSet = new DataSet();
----
---
dAd.Fill(dTable); // will also work
dAd.Fill(dSet); // will also work
We should only use DataSet as parameter when we are expecting more than one result set is being returned from database.
If you have more than one lakh rows in database table,while printing that table in frontend performence will be degraded. for that what do you do to improve the performence?
increase the execution time out in web.config (or)increase the connection time out in connection string
What is the purpose of connection pooling in ADO.NET?
Connection pooling enables an application to use a connection from a pool of connections that do not need to be re-established for each use. Once a connection has been created and placed in a connection pool, an application can reuse that connection without performing the complete connection creation process.
Difference Data Grid and Repeater
Datagrid is
* one which has advanced features and lets you do lot many things like paging and sorting your data without much effort.
* DataGrid can hold text data, but not linked or embedded objects.
Whereas a DataRepeater is
* which does not have the paging feature but we can do it by coding.
* one which can hold other controls and can embed objects.
* It can embed a Datagrid within it but not viceversa.
Apart from this a Data Repeater
--is used in places where you need more control over the rendering of your data
-- have very flexible templates that give you total control over the formatting of your data
Can you edit data in the Repeater control?
No, it just reads the information from its data source
Which ADO.NET object is very fast in getting data from database?
SqlDataReader object. (Note: Even datasets also use SqlDataReader objects internally for retriving data from database.)
Can you edit data in Repeater control?
No, it is readonly and forward only control so we can't edit data in repeater control.
Home
ASP.NET Interview Question Categories
No comments:
Post a Comment