SIGN UP MEMBER LOGIN:    
ARTICLE

DataAdapter Example in ADO.NET

Posted by Mahesh Chand Articles | ADO.NET in C# January 07, 2010
In this article I will explain about DataAdapter Example in ADO.NET
Reader Level:

This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

Now you'll create your first sample using data adapters. In this sample example, I'll show you how to create data adapters using Sql and OleDb data providers and fill data from data from data adapter to a DataGrid control.

First, create a Windows application using Visual C# projects and add two buttons and a DataGrid control to the form by dragging the controls form the toolbox to the form. Second, set both button's Name property; use OleDbDataAdapter and SqlDataAdapter. Next set the properties to OleDbData Adapter and SQL DataAdapter. After setting these properties, the form will look like figure 5-40. As you can see, there are two buttons, OleDbDataAdapter and SQL DataAdapter.

Now add button-click event handlers for both the OleDbDataAdapter and SQL DataAdapter buttons. You can add a button-click event handler either by double-clicking on the button or by using the Events tab of the properties window of a button control. On the OleDb DataAdapter button-click event handler, you'll write code to read data from an OleDb data source and fill data to the data grid. On the SQL DataAdapter button-click event handler, you'll write code to read data from a SQL Server datasource and fill data to the data grid.

Figure-5.40.jpg

Figure 5-40. Creating a Windows Forms application and adding controls to the form

Listing 5-44 shows the source code for the OleDb DataAdapter button click, and Listing 5-44 shows the source code for the SQL DataAdapter button click, and Listing 5-45 shows the source code for the SQL DataAdatper button click. As you can see, you follow the same steps as before. Open a connection, create a data adapter object with a SELECT string, create a dataset object, call data adapter's FILL method to fill the dataset, and bind the dataset to the DataGrid.DataSource property as DataSet.DefaultViewManager, which represents the default view of a DataSet object.

Listing 5-44. Displaying the Order table data in a Data Grid

using OleDbDataAdapter;

       
private void OleDbDataAdapter_Click(object sender, System.Event Args e)
       
{

       
//Create a connection object
       
string ConnectionString = @"provider=Microsoft.Jet.OLEDB.4.0;" +
       
"Data Source= C:/northwind.mdb";
       
string SQL = "SELECT * FROM Orders";
        OleDbConnection conn = new OleDbConnection(ConnectionString);

       
// open the connection
        conn.Open( );

       
// Create an OleDbDataAdapter object
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        adapter.SelectCommand = new OleDbCommand(SQL, conn);

       
// Create Data Set object
        DataSet ds = new DataSet("orders");
       
// Call DataAdapter's Fill method to fill data from the
       
// DataAdapter to the DataSet
        adapter.Fill(ds);

       
// Bind dataset to a DataGrid control
        dataGrid1.DataSource = ds.DefaultViewManager;
       
}

The output of Listing 5-44 looks like figure 5-41.

Figure-5.41.jpg

Figure 5-41. Filling data from an Access database to a DataGrid control using OleDbDataAdapter

The output of listing looks like figure 5-42.

Figure-5.42.jpg

Figure 5-42. Filling data from a SQL server database to a DataGrid control using SqlDataAdapter

Listing 5-45. Displaying the Customers tables data in a DataGrid


using
SqlDataAdapter;

        private void SqlDataAdapter_Click(object sender, System.EventArgs e)
        {
        string ConnectionString = "Integrated Security = SSPI;" +
        "Initial catalog = Northwind;" + " Data Source =MAIN-SERVER; ";
        string SQL = "SELECT CustomerID, CompanyName FROM Customers";
        SqlConnection conn = new SqlConnection(ConnectionString);

       
// open the connection
        conn.Open( );

       
//Create a SqlDataAdapter object
        SqlDataAdapter adapter = new SqlDataAdapter(SQL, conn);

       
// Call DataAdapter's Fill method to fill data from the
       
// Data Adapter to the DataSet
        DataSet ds = new DataSet("Customers");
        adapter.Fill(ds);
       
// Bind data set to a DataGrid control
        dataGrid1.DataSource = ds.DefaultViewManager;

        }

Conclusion

Hope this article would have helped you in understanding
DataAdapter Example in ADO.NET. See my other articles on the website on ADO.NET.

adobook.jpg This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor