Command, DataReader, and Parameter Object Reference

Chapter 5

Retrieving Data Using DataAdapter Objects

As you learned in the previous chapter, you can use Command objects and DataReader objects to execute queries and examine their results. But what if you want to store the results of a query in an ADO.NET DataSet object? You could write code to populate a DataSet with new rows by looping through the data available in a DataReader, like this:

Visual Basic .NET

Dim ds As New DataSet() Dim tbl As DataTable = ds.Tables.Add("Customers") 'Prepare DataTable. Dim cmd As New OleDbCommand() 'Prepare Command. Dim rdr As OleDbDataReader = cmd.ExecuteReader() Dim row As DataRow Do While rdr.Read()     row = tbl.NewRow()     row("CustomerID") = rdr("CustomerID")     'Fetch data from other columns.     tbl.Rows.Add(row) Loop rdr.Close()

Visual C# .NET

DataSet ds = new DataSet(); DataTable tbl = ds.Tables.Add("Customers"); //Prepare DataTable OleDbCommand cmd = new OleDbCommand(); //Prepare Command. OleDbDataReader rdr = cmd.ExecuteReader(); DataRow row; while (rdr.Read()) {     row = tbl.NewRow();     row["CustomerID"] = rdr["CustomerID"];     //Fetch data from other columns     tbl.Rows.Add(row); } rdr.Close();

Yikes! Storing the results of your query in a DataSet should be simple. This code isn't simple, and it certainly isn't RAD. Who wants to write code like that?

Thankfully, you don't have to. The ADO.NET object model offers a more elegant solution: using the DataAdapter object. In this chapter, you'll learn how to use this object to store the results of queries into DataSet objects and DataTable objects.



Microsoft ADO. NET Core Reference
Microsoft ADO.NET (Core Reference) (PRO-Developer)
ISBN: 0735614237
EAN: 2147483647
Year: 2002
Pages: 104
Authors: David Sceppa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net