SqlDataAdapter acts a bridge between an in-memory database tables and application. It is the SqlDataAdapter that manages connections with the data source and gives us disconnected behavior.  The SqlDataAdapter opens a connection only when required and closes it as soon as it has performed its task. For example, the SqlDataAdapter performs the following tasks when filling a DataSet with data:



  1. Open connection

  2. Retrieve data into DataSet or Data Table

  3. Close connection


 


The SqlDataAdapter holds the SQL commands and connection object for reading and writing data. You initialize it with a SQL select statement and connection object:The SqlDataAdapter contains all of the commands necessary to interact with the data source. There are two ways to add insert, update, and delete commands:  via SqlDataAdapterSqlCommandBuilder.  In this lesson, I'm going to show you the easy way of doing it with the SqlDataAdapter properties or with a


 


Example: Demonstrate Data Adapter


 


DataAdapter.aspx


 


<% @ Page Language ="C#" AutoEventWireup ="true" CodeFile ="DataAdapter.aspx.cs" Inherits ="DataAdapter" %>


 


<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


 


<html xmlns ="http://www.w3.org/1999/xhtml" >


<head runat ="server">


</head>


<body>


<form id ="form1" runat ="server">


<div>Data Adapter Object <br/>< br />


<asp:GridView ID="grdvEmployee" runat ="server" AutoGenerateColumns ="False" Font-Underline ="False">


<Columns>


<asp:BoundField DataField ="FirstName" HeaderText ="First Name" />


<asp:BoundField DataField ="LastName" HeaderText ="Last Name" />


<asp:BoundField DataField ="Address" HeaderText ="Address" />


<asp:BoundField DataField ="Title" HeaderText ="Title" />


</Columns>


</asp:GridView>


< br />


<span style ="text-decoration: underline"></ span >


<asp:Button ID ="btnDataAdapter" runat ="server" OnClick ="btnDataAdapter_Click" Text ="Filling GridView Using DataAdapter Object" Width ="256px" /></strong></div>


</form>


</body>


</html>


 


DataAdapter.aspx.cs


 


using System;


using System.Data;


using System.Configuration;


using System.Collections;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;


using System.Data.SqlClient;


 


public partial class DataAdapter : System.Web.UI. Page


{


 


  protected void btnDataAdapter_Click( object sender, EventArgs e)


    { // Using Data Adapter Object to fill GridView


    string strConn = "Data Source=localhost;Initial Catalog= Northwind; user Id=sa;Password = test " ;


    string sqlQuery = "select * from Employees" ;


    SqlConnection conn = new SqlConnection (strConn);


    //Create Data Adapter Instance


    SqlDataAdapter ada = new SqlDataAdapter (sqlQuery,conn);


    //Create Data Table Instance


    DataTable dtEmployee = new DataTable ();


    ada.Fill(dtEmployee);


    //GridView


    grdvEmployee.DataSource = dtEmployee.DefaultView;


    grdvEmployee.DataBind();


    }


}


 


Output:


 



                    

Copyright © 2010 VisualBuilder. All rights reserved