Friday, October 14, 2011

Connected and disconnected architecture in ADO.Net with Example

Posted by Rahul Kharde at 3:46 AM

Connected Architecture of ADO.NET
The architecture of, in which connection must be opened to access the data retrieved from database is called as connected architecture. Connected architecture was built on the classes connection, command, datareader and transaction. 

Connected architecture is when you constantly make trips to the database for any CRUD (Create, Read, Update and Delete) operation you wish to do. This creates more traffic to the database but is normally much faster as you should be doing smaller transactions.
Disconnected Architecture in ADO.NET

The architecture of in which data retrieved from database can be accessed even when connection to database was closed is called as disconnected architecture. Disconnected architecture of was built on classes connection, dataadapter, commandbuilder and dataset and dataview.
Disconnected architecture is a method of retrieving a record set from the database and storing it giving you the ability to do many CRUD (Create, Read, Update and Delete) operations on the data in memory, then it can be re-synchronized with the database when reconnecting. A method of using disconnected architecture is using a Dataset.

DataReader is Connected Architecture since it keeps the connection open until all rows are fetched one by one
DataSet is DisConnected Architecture since all the records are brought at once and there is no need to keep the connection alive
Difference between Connected and disconnected architecture

Connected Disconnected
It is connection oriented.
It is dis_connection oriented.
Datareader DataSet
Connected methods gives faster performance
Disconnected get low in speed and performance.
connected can hold the data of single table disconnected can hold multiple tables of data
connected you need to use a read only forward only data reader
disconnected you cannot
Data Reader can't persist the data
Data Set can persist the data
It is Read only, we can't update the data.
We can update data


Create Database “Student”

CREATE TABLE [dbo].[Student]
    [Name] [varchar](255) NULL,
    [Age] [int] NULL,
    [Address] [varchar](255) NULL

INSERT INTO Student([Name],[Age],[Address])VALUES('NAME 1','22','PUNE')
INSERT INTO Student([Name],[Age],[Address])VALUES('NAME 2','25','MUMBAI')
INSERT INTO Student([Name],[Age],[Address])VALUES('NAME 3','23','PUNE')
INSERT INTO Student([Name],[Age],[Address])VALUES('NAME 4','21','DELHI')
INSERT INTO Student([Name],[Age],[Address])VALUES('NAME 5','22','PUNE')


<html xmlns="">
<head id="Head1" runat="server">
    <title>Untitled Pagetitle>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966"
            BorderStyle="None" BorderWidth="1px" CellPadding="4">
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <RowStyle BackColor="White" ForeColor="#330099" />
            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <br />
        <asp:Button ID="Connected" runat="server" OnClick="Connected_Click" Text="Connected" />
        <asp:Button ID="Disconnected" runat="server" EnableTheming="False" OnClick="Disconnected_Click"
            Text="Disconnected" />

Code Behind

String StrSQL = "", StrConnection = "";
        protected void Page_Load(object sender, EventArgs e)
            StrSQL = "SELECT * FROM Student";
            StrConnection = "Data Source=ServerName;Initial Catalog=Database;User ID=Username;Password=password";

        protected void Connected_Click(object sender, EventArgs e)
            using (SqlConnection objConn = new SqlConnection(StrConnection))
                SqlCommand objCmd = new SqlCommand(StrSQL, objConn);
                objCmd.CommandType = CommandType.Text;
                SqlDataReader objDr = objCmd.ExecuteReader();
                GridView1.DataSource = objDr;

        protected void Disconnected_Click(object sender, EventArgs e)
            SqlDataAdapter objDa = new SqlDataAdapter();
            DataSet objDs = new DataSet();
            using (SqlConnection objConn = new SqlConnection(StrConnection))
                SqlCommand objCmd = new SqlCommand(StrSQL, objConn);
                objCmd.CommandType = CommandType.Text;
                objDa.SelectCommand = objCmd;
                objDa.Fill(objDs, "Student");
                GridView1.DataSource = objDs.Tables[0];


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!

Kindly Bookmark and Share it:



Dylan on November 26, 2011 at 10:36 PM said...

DataSet is disconnected and DataReader is connected. You can correct that inthe table.

Rahul K on November 27, 2011 at 10:15 PM said...
This comment has been removed by the author.

Vidya S on September 25, 2012 at 6:53 AM said...
This comment has been removed by the author.

Vidya S on September 25, 2012 at 6:56 AM said...

Don't you think we use DataAdapter in connected architecture?

Rahul K on September 26, 2012 at 1:11 AM said...

• SqlDataAdapter can work with disconnected and connected architecture ,
• SqlDataAdapter can directly update the DB(for instance change in datagridview can directly reflect database table) if used with SqlCommandBuilder
• SqlDataAdapter can fill DataTable,DataSet
• Sqlcommand can use Command.Executereader to read the data(which has better performance then Fill method of DataAdapter)

We can use data adapter in Connected architecture as below

SqlDataAdapter objDa;
DataTable dt;
using (SqlConnection objConn = new SqlConnection(StrConnection))
SqlCommand objCmd = new SqlCommand(StrSQL, objConn);
objDa = new SqlDataAdapter(objCmd);
dt = new DataTable();
GridView1.DataSource = dt;

abdullah raza Lakhan on January 5, 2013 at 11:28 AM said...

Nice effort and really appreciation

Anonymous said...

• SqlDataAdapter can work with disconnected and connected architecture ,

this line is true ???????

saravanan sundaram on April 11, 2013 at 11:19 PM said...

Hi Rahul

This is Saravanan, the notes and example which u posted is very much use full for me. Thank's a lot


Anonymous said...

in projects, when we use Connected & disconnected model

i.e Command & DataAdapter to update data


Rahul K on May 2, 2013 at 10:37 PM said...

Use Connected model when:

• For large / huge amount of data.
• The data is getting changed very frequently

Use Disconnected model when:

• The data is small /less
• It is not changing frequently or the application can handle the changes nevertheless.
• Frequent query to database is either not possible or perhaps is time/resource consuming.

H Max on April 10, 2015 at 9:42 PM said...

some people think before they speak.some people speak before they think.but the smart ones just smile and nod.
- My Citi Credit Card Account Login

Gianender S Bhardwaj on October 19, 2015 at 12:03 AM said...

thanks for very useful information........

nadendla naveen kumar on May 12, 2016 at 5:50 AM said...

Thank you for useful information given in Help To Understand.Net

Tanika Co Valda on October 31, 2017 at 9:30 AM said...

This is an amazing blog,it gives very helpful messages to us.Besides that Wisen has established as Best Dot Net Training in Chennai. or learn thru ASP.NET Online Training . Nowadays Dot Net has tons of job opportunities on various vertical industry.

Unknown on March 21, 2018 at 3:02 AM said...

Thanks for sharing this amazing blog
.Net Online Course

sanu Singh on May 9, 2018 at 2:37 AM said...

hi, Thanks for Sharing this Valuable Information.
.Net Training in Delhi

Have any question? Feel Free To Post Below:


Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top