This article describes you how to retrieve tables data from Sql Database in your ASP.NET applications with both "Connected" and "Disconnected" Mode and this article also clarify you which one is better in desire situation. Firstly going with What is Connected and Disconnected Mode, and After it I will write a code which help you to retrieve tables data from the Sql database in both mode.
Connected Mode
- Connected mode is connection oriented.
- In Connection mode, we read data from a database by using a DataReader object.
- Connected mode methods provide faster performance.
- Connected mode can hold the data of single table.
- Connected mode is read only, we can't update the data.
Disconnected mode
- Disconnected mode is disconnected connection oriented.
- In Disconnection mode, we read data from a database by using a DataSet object.
- Disconnection mode get low in speed and performance.
- Disconnection mode can hold multiple tables of data.
- we can perform all option as like update, insert, delete etc.
Why we use Disconnected mode
In Disconnected mode, we are used DataSet for retrieving data from database. so we are not need to maintaining the connection also. we can be performed all the operations with the data. It "wont cause traffic problem" while working with database.
Example
In this example, i will explain how to work with connected and disconnected mode in ADO.NET.
Program Code
Connection_Mode.aspx
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeFile="Connection_Mode.aspx.cs"
Inherits="Connection_Mode"
%>
<!DOCTYPE
html>
<html
xmlns="http://www.w3.org/1999/xhtml">
<head
runat="server">
<title></title>
</head>
<body>
<form
id="form1"
runat="server">
<h3
style="font-weight:
bold;
color:
#0000FF;">Connected
and Disconnected Mode in ASP.NET</h3>
<div>
<asp:Button
ID="Connected"
runat="server"
Text="Connected
Mode"
Font-Bold="True"
OnClick="Button1_Click1"
/>
<asp:Button
ID="Disconneted"
runat="server"
Text="Disconnected
Mode"
Font-Bold="True"
OnClick="Disconneted_Click"
/>
</div>
<div>
<br
/>
<asp:GridView
ID="gdview"
runat="server"
HeaderStyle-BackColor="#669999"
HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="White"
>
</asp:GridView>
</div>
<p>
</p>
</form>
</body>
</html>
Connection_Mode.aspx.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
public
partial
class
Connection_Mode
: System.Web.UI.Page
{
SqlConnection
con;
SqlCommand
cmd;
DataTable
dt;
// Use for Connected mode
SqlDataReader
dr;
SqlDataAdapter
da;
// Use for Disconnected Mode
DataSet
ds;
protected
void
Page_Load(object
sender,
EventArgs
e)
{
}
protected
void
Button1_Click1(object
sender,
EventArgs
e)
{
// Connected Mode
con =
new
SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
cmd =
new
SqlCommand("select
* from LoginDetail",
con);
dr = cmd.ExecuteReader();
dt =
new
DataTable();
dt.Load(dr);
gdview.DataSource = dt;
gdview.DataBind();
con.Close();
}
protected
void
Disconneted_Click(object
sender,
EventArgs
e)
{
// Disconneted Mode
using
(con =
new
SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using
(da =
new
SqlDataAdapter("select
* from EmpSalary_Info",
con))
{
ds =
new
DataSet();
da.Fill(ds);
gdview.DataSource = ds.Tables[0];
gdview.DataBind();
}
}
}
}
Output 1
Output 2
Click on Connected button
Output 3
Click on Disconnected button