SIGN UP MEMBER LOGIN:    
ARTICLE

DataAdapter in ADO.NET

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

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

DataAdapter: Adapting to Your Environment

As you're seen in figure 5-1, a DataAdapter plays a vital role in the ADO.NET architecture. It sits between a data source and a dataset and passes data from the data source to the dataset, and vice versa, with or without using commands. Now is the time you'll be using disconnected classes such as DataSet, DataTable, DataView, and DataViewManager to write Windows Forms and Web Forms  based interactive database GUI applications.

The DataAdapter class for all data providers comes from the DbDataAdapter class, which in turn comes from the DataAdapter class.

An application doesn't create an instance of the DbDataAdapter interface directly, but instead it creates an instance of a class that inherits IdbDataAdapter and DBDataAdapter. As you can see from Figure 5-39, many data provider – specific classes implement IDbDataAdapter.

figure-.39.gif

Figure 5-39. Data Provider–specific classes implement IDbDataAdapter

The DataAdapter enables you to connect to a dataset and specify SQL strings for retrieving data from or writing data to a DataSet. As you've seen in the beginning of this article, a dataset represents in–memory cached data. An in memory object frees you from the confines of the specifics of database and allows you to deal with the data in memory. The DataAdapter serves as an intermediary between the database and the DataSet.

Constructing a DataAdapter Object

The DataAdapter constructor has many overloaded forms. You can create a constructor with no arguments, pass a Command object, pass a Command object with Connection object as arguments, or any combination of these. You can also specify a SQL statement as string for querying a particular table or more than one table. You can also specify the connection string or a Connection object to connect to the database.

Listing 5-41 creates a connection, builds a SQL statement using the SELECT query, and passes the SQL string and the connection objects as SqlDataAdapter constructor arguments. I've been using in the previous examples.

Listing 5-41. Executing a SELECT statement using SqlDataAdapter

string
ConnectionString = "Integrated Security = SSPI;" +
"Initial catalog = Northwind; "
+ " Data Source = localhost; ";
string
SQL = "SELECT CustomerID, CompanyName FROM Customers";
SqlConenction conn = new SqlConnection(ConnectionString);


// open the connection

conn.Open();


// Create a SqlDataAdapter object

SqlDataAdapter adapter = new SqlDataAdapter(SQL, conn);


As discussed earlier, there is no difference in creating OleDb, Sql, or ODBC data adapters. The only difference is the connection string. For example, the following code snippet shows you how to create an OleDbDataAdapter object.

Listing 5-42 uses the Access2000 North wind database and accesses all records of the Orders table by using a SELECT *SQL query.

Listing 5-42. Executing a SELECT statement using OleDbDataAdapter

// 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 objecto

OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, conn);


You can also use DataAdapter's Command properties by using the Command object with OleDbDataAdapter. For example, the following code uses OleDbCommand to set the SelectCommand property of the data adapter. You can also see that OleDbDataAdapter has no arguments as its constructor:

// Create an OleDbDataAdapter object

OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand (SQL, conn);


Conclusion

Hope this article would have helped you in understanding DataAdapter 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
 

Hi! Im beginner and my problem is propably very easy,but i need help. I want to show data from database on GridView. I use following code:


  SqlConnection mainConnection = new SqlConnection("Integrated SEcurity=true;Initial Catalog=Northwind;Data Source=computer-name\\SQLEXPRESS");
mainConnection.Open();
SqlDataAdapter Adapter = new SqlDataAdapter("select orderID FROM orders", mainConnection);
DataSet newDataSet = new DataSet();
Adapter.Fill(newDataSet);
dataGridView1.DataSource = newDataSet.DefaultViewManager;

but it doesnt work.  What's wrong?

Thanks for help.

Kamil

Posted by Kamil Bedkowski Jul 10, 2010
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.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor