ADO.NET is a technology used for data and is provided by the Microsoft .NET Framework. It is a part of the .NET framework that supports the communication between relational and non-relational systems with the help of a set of software components. It supports disconnected architecture using which programmers are allowed to access data and data services from a database without depending on the data source.
ADO.NET is comprised of a group of built-in classes that are useful for establishing the database connection, for gaining access to XML, relational data and application data, and for retrieval of a result. It can be used in various programming languages such as Visual Basic.NET, Visual C++, etc., that are supported by the .NET framework.
ADO.NET has various advantages which can be categorized into the following categories:
ADO.NET being one of the products of Microsoft, it is good enough to position itself strong in the market. ADO.NET has a massive community support, so it is definitely having a large scope ahead. You could learn ADO.NET along with hands-on experience on .Net framework in order to have a good scope. Any full-stack developer who has a better grip over both front-end and back-end technology can precisely learn ADO.NET.
System.Data.dll
.ADO | ADO.NET |
---|---|
It is Component Object Modelling(COM) based. | It is Common Language Runtime(CLR) based. |
It works in connected mode to access the data store. | It does require an active connection, works in disconnected mode to access the data store. |
It uses the RecordSet object to access and store data from the data sources. | It uses a DataSet object to access and store data from the data sources. |
It provides a feature of locking. | It does not provide a feature of locking. |
Data is stored in binary form. | Data is stored in XML. |
It does not support XML integration. | It supports XML integration. |
Using a single connection instance, it is not possible to send multiple transactions. | Using a single connection instance you can send multiple transactions. |
We can create only client-side cursors. | Both client-side and server-side cursors can be created. |
It supports sequential row access in a RecordSet. | Non-sequential data access is supported in DataSet by using a collection-based hierarchy. |
It will make use of SQL JOINs and UNIONs for combining data from multiple tables. It is not possible to fetch records from multiple tables independently. | It will make use of DataRelational objects to combine data from multiple tables without the help of JOINs and UNIONs. Therefore records from multiple tables are maintained independently. |
Here, DataAdapter will receive the data from the items table and fills the DataSet, which will be later used to display the information retrieved from the items database.
ADO.NET(ActiveX Data Objects) | ASP.NET(Active Server Pages) |
---|---|
ADO.NET is a Library within the .NET framework. | ASP.NET is a Framework. |
It is a technology useful for accessing data from databases. | It is a technology useful for the creation of dynamic web pages. |
Here, data can be converted into XML format. | Here, We can write our code into VB.Net, C#, ASP.Net, etc. |
It is used to develop reliable and scalable database applications with high performance for client-server applications. | It is used to create dynamic web pages, web applications, websites, and web services. |
DataSet can be said as a collection of database tables(row and column format) that holds the data. There are two types of DataSet in ADO.NET. They are:
Example program for the usage of DataSet:
Here, DataSet will be filled by DataAdapter that receives data from the employee table. This DataSet will be used to display the information received from the employee database.
DataTable | DataSet |
---|---|
DataTable consists of a single database table that is placed within a memory. | DataSet consists of a collection of multiple database tables which is placed within a memory. |
It has a row and column collection. | It has a database table collection. |
It allows fetching only a single TableRow at a time. | It allows fetching multiple TableRows at a time. |
It is a single database table, so there will not be any relation with other tables. | It represents a collection of DataTable objects, so there might be a relation between them to obtain a particular result. |
In this, DataSource objects are not serialized. | In this, DataSource objects are serialized. |
UniqueConstraint and ForeignKeyConstraint objects are not available enforcing data integrity. | UniqueConstraint and ForeignKeyConstraint objects are available for enforcing data integrity. |
Various namespaces available under ADO.NET is given below:
System.Data
: It contains the definition for rows, columns, relations, views, tables, constraints, and databases.System.Data.SqlClient
: It is a collection of classes that are helpful in connecting to a Microsoft SQL Server database such as SqlConnection, SqlCommand, SqlDataAdapter, etc.System.Data.Odbc
: It consists of classes that are required for connecting with most Odbc Drivers. These classes include OdbcConnection, OdbcCommand.System.Data.OracleClient
: It has classes required for connection with an Oracle database, OracleConnection, OracleCommand.Object pooling is a repository of the objects in memory that can be reused later without creating them. This object pooling reduces the burden of creating objects when it is required. Whenever there is a requirement of an object, the object pool manager will process the request and serve accordingly. It is designed for optimizing the use of limited resources so that the demands of client requests will be fulfilled.
DataSet | DataReader |
---|---|
DataSet provides read/write access to data, so we can update the data. | DataReader provides read-only access to data, so we can't update the data. |
It has a disconnected architecture, which means the data obtained from the database can be accessed even after the database connection was closed. | It has a connected architecture, which means to access the data retrieved from the database, the connection must be opened. |
It supports various database tables from different databases. | It supports only a single table from a single database. |
It provides slower access to data due to overhead. | It provides faster access to data. |
Both forward and backward scanning of data is possible. | Only forward scanning of data is possible. |
Different execute() methods supported by SqlCommandObject in ADO.NET is given below:
ExecuteScalar()
: This method returns only a single value from the first row and first column of the ResultSet after the execution of the query. Even if ResultSet is having more than one row or column, all those rows and columns will be ignored. If the ResultSet is empty, it will return NULL.ExecuteNonQuery()
: This method returns the number of rows affected by the execution of a query. This method is not useful to return the ResultSet.ExecuteReader()
: This method returns an object of DataReader which is a read-only and forward-only ResultSet. It needs a live connection with the Data Source. We cannot directly instantiate the DataReader object. A valid DataReader object can be created with the help of the ExecuteReader() method.ExecuteXmlReader()
: This method builds an object of the XmlReader class and will return the ResultSet in the form of an XML document. This method is made available in SQL Server 2000 or later.In ADO.NET, transactions are used when you want to bind several tasks together and execute them in the form of a single unit. The transaction provides data consistency by ensuring either all of the database operations will be succeeded or all of them will be failed. For example, consider an application that performs two tasks. First, it updates an item_order table with order information. Second, it updates an item_inventory table that holds inventory information, where a number of items ordered will be debited. If any one of the tasks fails, then both updates must be rolled back.
Two types of transactions supported by ADO.NET are as follows:
System.Data.SqlClient
namespace. Similarly, to perform an Oracle transaction, import the System.Data.OracleClient
namespace. A DbTransaction
class will be used for writing code that is independent of the provider and that requires transactions.OLE DB | ODBC |
---|---|
An API(Application Programming Interface) that allows accessing data from different sources in a uniform manner. | It is an API for accessing DBMS (DataBase Management System). |
It supports both relational and non-relational databases. | It supports only relational databases. |
It is procedural-based. | It is component-based. |
It is easier to deploy. | It is difficult to deploy. |
It gives a higher performance on loading and extracting the data. | It performs less compared to OLE DB on loading and extraction of data. |
OleDbConnection = New OleDbConnection(connetionString) is used to make connection with OLE DB data source. |
resource odbc_connect(string datasource , string username , string password , [int cursor_type ]) is used to make a connection to an ODBC data source. On success, this function will return a connection resource handle that is helpful in accessing the database using subsequent commands. |
The task of grouping database connections in the cache memory is to make them available whenever there is a requirement of connection. Opening a new database connection every time is a time-consuming process. Connection pooling allows you to reuse existing and active database connections, whenever there is a need, and thus increases the application performance.
By setting the pooling property into true or false in the connection string, we can enable or disable the connection pooling in the application. It is enabled by default in every application.
DataTable in ADO.NET represents a single table in a DataSet that has in-memory relational data. The data within DataTable is local to the .NET framework-based application to which it belongs but can be populated using a DataAdapter from different data sources such as Microsoft SQL Server. The DataTable class belongs to the System.Data
namespace within the library of .NET Framework.
DataTable can be represented in .aspx.cs
code as given below:
The SQL connection and SQL command object will be created. We pass the SQL query to the object of SQL command class. A new data table object will be created by using the DataTable class and it is filled with data using a data adapter.
Some of the properties provided by the DataReader are as follows:
Some of the methods provided by the DataReader are as follows:
The conditions for connection pooling are:
Data providers are used to transferring the data between the client application and the data store. It encapsulates the database-specific details. Data providers are helpful for database connection, data retrieval, storing the data in a dataset, reading the retrieved data, and updating the database.
The data providers that comes along with the ADO.NET Framework are:
System.Data.OleDb
namespace. This provider can be used to access Microsoft Access, DB2/400, SyBase, and SQL Server 6.5 and earlier.System.Data.Odbc
namespace. This provider is used when there will not be any newer provider is available.System.Data.SqlClient
namespace. Classes available under this provider will provide the same functionality as the generic OleDb provider.The reasons for using Stored Procedures in ADO.NET are given below:
ADO.NET is based on an Object Model where data residing in the database is accessed using a data provider. It is a technology of data access given by the Microsoft .Net Framework, which helps to communicate between relational and non-relational systems using a common group of components.
The components of ADO.NET architecture are:
Connected architecture:
Disconnected architecture:
Here, we create an object of the class SqlConnection and SqlCommand. We pass SQL Statement to the object of SqlCommand class, which returns a single value. When ExecuteScalar() function gets executed, a single value will be returned, i.e, the total salary of employees. This value will be displayed using a message box.
There are seven main objects in ADO.NET. They are:
System.Data.ADO
and the System.Data.SQL
namespaces. DataSet is a database cache built-in memory for using it in disconnected operations. It holds the complete collection of tables, constraints, and relationships.Before performing any task in the database, SQL Server will authenticate. Two types of authentication techniques are:
Windows Authentication: This default authentication is provided only through Windows domain accounts. This SQL Server security model is strongly integrated with Windows, so it is also referred to as integrated security. Particular Windows users and group accounts are allowed to login into SQL Server. Windows users who are already been authenticated or logged onto Windows do not have to provide additional credentials.
The below-given SqlConnection.ConnectionString
specifies Windows authentication without any need of providing a user name or password by the user.
SQL Server and Windows Authentication Mode(Mixed-mode): Authentication will be provided with the help of the Windows and SQL Server Authentication combination. User name and password pair will be maintained within SQL Server. In order to use this mixed-mode authentication, you need to create SQL Server logins that are stored in SQL Server. After that, you can supply the user name and password to SQL Server at run time.
The below-given ConnectionString specifies Mixed mode authentication:
Response.Expires
and Response.ExpiresAbsolute
property?Response.Expires
property is specific to the minutes that a particular page stays in the cache for the specific time from the time it has been requested. For example, if Response.Expires
value is set to 5 minutes, then the page is instructed to be in cache for 5 minutes from the time it has been requested.Response.ExpiresAbsolute
property helps to provide the proper time at which a specific page cache has been expired. For example, Response.ExpiresAbsolute
provides information like 14 March 15:40:15. This time tells about when the page was in cache.After tables have been added into a DataSet, the below-given code tells about how to make use of the DataSet tables. If you decide to use the first table in a dataset or to copy the table data into a data table, then follow the below-given code:
The above code can be used to add the required number of tables in a dataset. This ensures connection-less access to data. As the dataset is filled with multiple tables, every time we want to query the data the database connection is not required. It also makes sure about the reusability of data.
Connected architecture | Disconnected architecture |
---|---|
It is connection-oriented. | It is not connection-oriented. |
DataReader is a connected architecture. | DataSet is a disconnected architecture. |
High speed and performance are given by connected methods. | Disconnected methods are low in speed and performance. |
Data persistence is not possible using DataReader. | Data persistence is possible using DataSet. |
It carries the single table data. | It carries data from multiple tables. |
We can't update the data as it is read-only. | Here we can update the data. |
The DataSet object has two methods to track down the changes:
Use the RejectChanges() method, if you want to reverse the entire changes since from the time the DataSet object was loaded.
The collection of rows for the DataTable object has been defined by the DataRowCollection class. DataRowCollection class has the method NewRow() for adding a new DataRow to DataTable. This method creates a new row that implements the similar schema that is applied to the DataTable.
The methods provided by the DataRowCollection object are given below:
Consider the below example where a connection to the SQL Server has been established. An employee database will be used to connect. The C# code will be:
Using block will be useful in closing the connection automatically. It is not required to explicitly call the close() method, because using block will do this implicitly when the code exits the block.
Output:
On execution, if the connection has been established, a message will be displayed on an output window.
If the connection is not created with the help of using a block, a connection must be closed explicitly.
Serialization is the method of converting an object into a byte stream which can be stored as well as transmitted over the network. The advantage of serialization is that data can be transmitted in a cross-platform environment across the network and also it can be saved in a storage medium like persistent or non-persistent.
The code for serializing a DataSet is:
In the above given example, the database name is employee and, the table name is emp. The data in a DataSet will be serialized and stored in a demo.xml file by using Serialize() method.
Output:
ADO.NET is a brilliant technology that was developed by Microsoft on the framework of .NET. The primary roles and responsibilities of ADO.NET technology is to setup a bridge between backend language and your database. A good experience of this technology will be of great use from a development point of view.
ADO.NET technology will definitely help in your career growth as it has quite a good scope. Also learning this interesting technology will always be of great fun. ADO.NET along with the knowledge of database will definitely be exceptional from a growth perspective.