SIGN UP MEMBER LOGIN:    
ARTICLE

Working With SqlDataAdapter Class in ADO.NET

Posted by Alok Pandey Articles | ADO.NET in C# December 30, 2011
In this article you will learn about the SqlDataAdapter class and its two important methods - Fill() and Update().
Reader Level:

Introduction

The SqlDataAdapter class is found in the System.Data.SqlClient namespace. It is a very important class in the .NET framework. It works as a bridge between DataSet and Database. It opens the database connection, executes the SQL statements and closes the connection at last. There is no need to open and close the connection. It has two more important methods Fill() and Update(). The Fill() method is used to fill the DataSet or DataTable and Update() is used for saving the changes to the Database in same order as the DataSet, which is made with DataSet.

Now we will work with SqlDataAdapter in our application. I have a Database student and a Database table as "student_detail" which has one record. We take a window form application in Visual Studio 2010 > Take 2 button and 1 DataGridView control and write the following code.

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Data.SqlClient;
 

namespace
adapter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter adapter;
        DataSet ds;
        DataTable dt;
        string connstring = "database=student;server=.;user=sa;password=wintellect";
        private void Form1_Load(object sender, EventArgs e)
        {
          
        }
        private void btndataset_Click(object sender, EventArgs e)
        {
            // Using DataSet
 
            adapter = new SqlDataAdapter("select * from student_detail", connstring);
            ds = new DataSet();
            adapter.Fill(ds);
            dGVshowrecord.DataSource = ds.Tables[0];
 
            //********
            // Same operation can be performed by below code
            //********
 
            //adapter = new SqlDataAdapter();
            //SqlConnection con = new SqlConnection(connstring);
            //adapter.SelectCommand = new SqlCommand("select * from student_detail", con);
            //ds = new DataSet();
            //adapter.Fill(ds);
            //dGVshowrecord.DataSource = ds.Tables[0];
        }
        private void btndatatable_Click(object sender, EventArgs e)
        {
            // Using DataTable
 
            adapter = new SqlDataAdapter("select * from student_detail", connstring);
            dt = new DataTable();
            adapter.Fill(dt);
            dGVshowrecord.DataSource = dt;
 
            //********
            // Same operation can be performed by below code
            //********
 
            
//adapter = new SqlDataAdapter();
 
            //SqlConnection con = new SqlConnection(connstring);
 
            // adapter.SelectCommand = new SqlCommand("select * from student_detail", con);
 
            //dt = new DataTable();
 
            //adapter.Fill(dt);
 
           
//dGVshowrecord.DataSource = dt;
        }
    }
}
 
Run the application.

Output

sqldataadapter

Click on both buttons to show records from "student_detail" table.

sqldataadapter in .net

Now we insert, delete and update records using the SqlDataAdapter class. We will use the update() method of SqlDataAdapter and also we have to use the SqlCommandBuilder class for performing such an operation. Add 2 buttons in your application and set their text property to "Clear" and "Update". Replace the preceding code with the following code. 

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;

using
System.Data.SqlClient;
 

namespace
adapter
{
    public partial class Form1 : Form
    {
       public Form1()
        {
           InitializeComponent();
        }
        SqlDataAdapter adapter;
        DataSet ds;
        DataTable dt;
        string connstring = "database=student;server=.;user=sa;password=wintellect";
        private void Form1_Load(object sender, EventArgs e)
        {        

        }
        private void btndataset_Click(object sender, EventArgs e)
        {
            // Using DataSet
            adapter = new SqlDataAdapter("select * from student_detail", connstring);
            ds = new DataSet();
            adapter.Fill(ds);// fill the DataSet
            dGVshowrecord.DataSource = ds.Tables[0];// Binding DataGridView with DataSet
         }
        private void btnupdate_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder cmb = new SqlCommandBuilder(adapter);// Creating instance of SqlCommandBuilder
            adapter.Update(ds);// updating changes
        }
        private void btnclear_Click(object sender, EventArgs e)
        {
            ds.Clear();// Clear the DataSet
        }
    }
}

Run the application

Output

sqldataadapter in ado.net

Click the "Using Dataset" button. It will show record from Database.

sqldataadapter class in .net

Make some changes in records of DataGridView. I have made some changes by adding some new row and updating course of first row. Click the "Update" button. It will save the current record of DataSet into Database.

sqldataadapter class in ado.net

Now click the Clear button to clear the DataSet.

sqldataadapter class in .net

Now click the "Using Dataset" button. It will show the record from the Database.


sqldataadapter class in .net

Here are some related resources

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor