Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
Connected and Disconnected Architecture in ADO.NET
Connected and Disconnected Architecture in ADO.NET
In this article, I am going to discuss What is Connected and Disconnected Architecture in ADO.NET with Examples. Please read our previous article where we discussed How to Perform Batch Operations in C# Using ADO.NET Data Adapters with Examples.
Connected vs Disconnected Architecture in ADO.NET
The ADO.NET is one of the Microsoft data access technologies which is used to establish a connection between the .NET Application (Console, WCF, WPF, Windows, MVC, Web Form, etc.) and different data sources such as SQL Server, Oracle, MySQL, XML, etc. The ADO.NET framework access the data from data sources in two different ways. The models are Connection Oriented Data Access Architecture and Disconnected Data Access Architecture. In this article, I will explain both these architectures in detail with Examples.
Types of Architecture to Access the Data using ADO.NET:
The Architecture supported by ADO.NET for communicating with data sources is categorized into two models. They are as follows:
- Connected Oriented Architecture
- Disconnected Oriented Architecture
So, ADO.NET supports both Connection-Oriented Architectures as well as Disconnection-Oriented Architecture. Depending upon the functionality or business requirement of an application, we can make it either Connection-Oriented or Disconnection-Oriented. Even, it is also possible to use both Architectures together in a single .NET application to communicate with different data sources.
ADO.NET Connection-Oriented Data Access Architecture:
In the case of Connection Oriented Data Access Architecture, always an open and active connection is required in between the .NET Application and the database. An example is Data Reader and when we are accessing the data from the database, the Data Reader object requires an active and open connection to access the data, If the connection is closed then we cannot access the data from the database and in that case, we will get the runtime error.
The Connection Oriented Data Access Architecture is always forward only. That means using this architecture mode, we can only access the data in the forward direction. Once we read a row, then it will move to the next data row and there is no chance to move back to the previous row.
The Connection Oriented Data Access Architecture is read-only. This means using this architecture we can only read the data. We cannot modify the data i.e. we cannot update and delete the data row.
For Connection Oriented Architecture, we generally use the object of the ADO.NET DataReader class. The DataReader object is used to retrieve the data from the database and it also ensures that an open and active connection should be there while accessing the data from the database. In Connection Oriented Architecture, the .NET Application is directly linked with the corresponding Database.
ADO.NET DataReader in Connected-Oriented Architecture
The ADO.NET DataReader object is used to read the data from the database using Connected Oriented Architecture. It works in forward only and only mode. It requires an active and open connection while reading the data from the database.
Example to Understand Connection-Oriented Architecture:
We are going to use the following Employee tables to understand Connection-Oriented Architecture as well as Disconnection-Oriented Architecture using ADO.NET.
Please use the following SQL Script to create the EmployeeDB and populate the Employee table with the required sample data.
Using ADO.NET Data Reader to Fetch the Data from the Database:
In the below example, I am using the ADO.NET Data Reader object to Fetch the Data from the Database. As Data Reader in ADO.NET works on Connection-Oriented Architecture, so it always requires an active and open connection to access the data from the database.
Output:
Now, let us do one thing. After reading the first row from the database, let us close the connection and see what happens. In the below example, you can see, within the while loop, after reading the first row, we are closing the database connection by calling the Close method.
Output:
As you can see in the above output, after reading the first row, the data reader throws an exception, and the reason the database connection is closed. So, this proves that connection-oriented architecture always requires an active and open connection to the database.
ADO.NET Disconnection-Oriented Data Access Architecture:
In the case of Disconnection Oriented Data Access Architecture, always an open and active connection is not required in between the .NET Application and the database. In this architecture, Connectivity is required only to read the data from the database and to update the data within the database.
An example is DataAdapter and DataSet or DataTable classes. Here, using the DataAdapter object and using an active and open connection, we can retrieve the data from the database and store the data in a DataSet or DataTable. The DataSets or DataTables are in-memory objects or you can say they store the data temporarily within .NET Application. Then whenever required in our .NET Application, we can fetch the data from the dataset or data table and process the data. Here, we can modify the data, we can insert new data, can delete the data from within the dataset or data tables. So, while processing the data within the .NET Application using DataSet or Datatable, we do not require an active and open connection.
And finally, when we processed the data in our .NET Application, then if we want to update the modified data which is stored inside the dataset or Datatable into the database, then we need to establish the connection again and we need to update the data in the database. This is how Disconnection Oriented Data Access Architecture works.
ADO.NET DataAdapter in Disconnection-Oriented Architecture
The ADO.NET DataAdapter object acts as an interface between the .NET application and the database. The Data Adapter object fills the Dataset or DataTable which helps the user to perform the operations on the data. And once we modify the DataSet or DataTable, then we need to pass the modified DataSet or DataTable to the DataAdapter which will update the modified data into the database. The DataAdapter object will internally manage the connection i.e. when to establish the connection and when to terminate the connection.
The ADO.NET DataAdapter establishes a connection with the corresponding database and then retrieves the data from the database and fills the retrieved data into the Dataset or DataTable. And finally, when the task is completed i.e. the Data is processed by the application i.e. the data is modified by the application, and modified data is stored in the DataSet or DataTable. Then the DataAdapter takes the modified data from the DataSet or DataTable and updates it into the database by again establishing the connection.
So, we can say that DataAdapter acts as a mediator between the Application and database which allows the interaction in disconnection-oriented architecture.
Example to Understand Disconnected-Oriented Architecture in ADO.NET:
In the below example, I am using the ADO.NET Data Adapter object to Fetch the Data from the Database and fill the DataTable. Then data table stores the data in memory and then we modified the data table data, and finally, we provided the modified data table with the Data Adapter object which will update the modified data within the database.
This is how connection-oriented architecture works. Now, you can verify the Employee data and you should see the updated data as shown in the below image.
Differences Between Connected-Oriented Architecture and Disconnected-Oriented Architecture:
Let us see the Differences Between ADO.NET Connected Oriented Architecture and Disconnected Oriented Architecture.
ADO.NET Connected Oriented Architecture:
- It is connection-oriented data access architecture. It means always an active and open connection is required.
- Using the DataReader object we can implement the Connected Oriented Architecture.
- Connected Oriented Architecture gives a faster performance.
- Connected Oriented Architecture can hold the data of a single table only.
- You can access the data in a forward-only and read-only manner.
- Using Data Reader, we cannot persist the data in the database.
ADO.NET Disconnected Oriented Architecture:
- It is disconnection-oriented data access architecture. It means always an active and open connection is not required.
- Using DataAdapter and DataSet or Datatable we can implement Dis-Connected Oriented Architecture.
- Disconnected Oriented Architecture gives a lower performance.
- Disconnected Oriented Architecture can hold the data of multiple tables using dataset and single table data using Datatable.
- You can access the data in forward and backward directions and you can also modify the data.
- Using Data Adapter, we can persist the DataSet or DataTable data into the database.
In the next article, I am going to discuss How to Load XML data to SQL Server Database Table using ADO.NET DataTable with Examples. Here, in this article, I try to explain Connected and Disconnected Architecture in ADO.NET with Examples. I hope you enjoy this Connected and Disconnected Architecture in ADO.NET article.