Tải bản đầy đủ (.pdf) (5 trang)

Tài liệu Performing a SQL SELECT Statement and Storing the Rows Locally phần 2 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (19.54 KB, 5 trang )

The Fill() method then creates a DataTable in the DataSet with the specified name and
runs the SELECT statement. The DataTable created in your DataSet is then populated
with the rows retrieved by the SELECT statement.
The following example calls the Fill() method of mySqlDataAdapter, passing myDataSet
and "Customers" to the Fill() method:
mySqlDataAdapter.Fill(myDataSet, "Customers");
The Fill() method creates a DataTable object named Customers in myDataSet and
populates it with the rows retrieved by the SELECT statement. You can access these
rows, even when disconnected from the database.
Step 11: Close the Database Connection
Close the database connection using the Close() method of the SqlConnection object
created in the first step. For example:
mySqlConnection.Close();


Note Of course, you don't have to immediately close the database connection before
reading locally stored rows from your DataSet. I close the connection at this point
in the example to show that you can indeed read the locally stored rows- even when
disconnected from the database.
Step 12: Get the DataTable Object From the DataSet Object
Get the DataTable object created in step 10 from the DataSet object.
You get a DataTable from your DataSet using the Tables property, which returns a
DataTableCollection object. To get an individual DataTable from your DataSet, you pass
the name of your DataTable in brackets ("Customers", for example) to the Tables
property. The Tables property will then return your requested DataTable, which you can
store in a new DataTable object that you declare. In the following example,
myDataSet.Tables["Customers"] returns the Customers DataTable created in myDataSet
in step 10, and stores the returned DataTable in myDataTable:
DataTable myDataTable = myDataSet.Tables["Customers"];



Note You can also specify the DataTable you want to get by passing a numeric value to
the Tables property. For example, myDataSet.Tables[0] also returns the Customers
DataTable.
Step 13: Display the Columns for Each Row in the DataTable
Display the columns for each row in the DataTable, using a DataRow object to access
each row in the DataTable. The DataTable class defines a property named Rows that
returns a DataRowCollection object containing the DataRow objects stored in that
DataTable. You can use the Rows property in a foreach loop to iterate over the DataRow
objects. For example:
foreach (DataRow myDataRow in myDataTable.Rows)
{
// ... access the myDataRow object
}
Each DataRow object stores DataColumn objects that contain the values retrieved from
the columns of the database table. You can access these column values by passing the
name of the column in brackets to the DataRow object. For example,
myDataRow["CustomerID"] returns the value of the CustomerID column.
In the following example, a foreach loop iterates over the DataRow objects in
myDataTable, and the column values are displayed for each row:
foreach (DataRow myDataRow in myDataTable.Rows)
{
Console.WriteLine("CustomerID = "+ myDataRow["CustomerID"]);
Console.WriteLine("CompanyName = "+ myDataRow["CompanyName"]);
Console.WriteLine("ContactName = "+ myDataRow["ContactName"]);
Console.WriteLine("Address = "+ myDataRow["Address"]);
}
As you can see, the name of each column is passed in brackets to each DataRow object,
which then returns the column value.

Note You can also specify the column you want to get by passing a numeric value in

brackets. For example, myDataRow[0] also returns the CustomerID column value.
Putting It All Together
Listing 5.1
shows a complete program that uses these steps. This program is named
SelectIntoDataSet.cs and is located in the ch05 directory.
Listing 5.1: SELECTINTODATASET.CS

/*
SelectIntoDataSet.cs illustrates how to perform a SELECT statement
and store the returned rows in a DataSet object
*/

using System;
using System.Data;
using System.Data.SqlClient;

class SelectIntoDataSet
{
public static void Main()
{
// step 1: formulate a string containing the details of the
// database connection
string connectionString =
"server=localhost;database=Northwind;uid=sa;pwd=sa";

// step 2: create a SqlConnection object to connect to the
// database, passing the connection string to the constructor
SqlConnection mySqlConnection =
new SqlConnection(connectionString);


// step 3: formulate a SELECT statement to retrieve the
// CustomerID, CompanyName, ContactName, and Address
// columns for the first ten rows from the Customers table
string selectString =
"SELECT TOP 10 CustomerID, CompanyName, ContactName, Address "+
"FROM Customers " +
"ORDER BY CustomerID";

// step 4: create a SqlCommand object to hold the SELECT statement
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

// step 5: set the CommandText property of the SqlCommand object to
// the SELECT string
mySqlCommand.CommandText = selectString;

// step 6: create a SqlDataAdapter object
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

// step 7: set the SelectCommand property of the SqlAdapter object
// to the SqlCommand object
mySqlDataAdapter.SelectCommand = mySqlCommand;
// step 8: create a DataSet object to store the results of
// the SELECT statement
DataSet myDataSet = new DataSet();

// step 9: open the database connection using the
// Open() method of the SqlConnection object
mySqlConnection.Open();

// step 10: use the Fill() method of the SqlDataAdapter object to

// retrieve the rows from the table, storing the rows locally
// in a DataTable of the DataSet object
Console.WriteLine("Retrieving rows from the Customers table");
mySqlDataAdapter.Fill(myDataSet, "Customers");

// step 11: close the database connection using the Close() method
// of the SqlConnection object created in Step 1
mySqlConnection.Close();

// step 12: get the DataTable object from the DataSet object
DataTable myDataTable = myDataSet.Tables["Customers"];

// step 13: display the columns for each row in the DataTable,
// using a DataRow object to access each row in the DataTable
foreach (DataRow myDataRow in myDataTable.Rows)
{

Console.WriteLine("CustomerID = "+ myDataRow["CustomerID"]);
Console.WriteLine("CompanyName = "+ myDataRow["CompanyName"]);
Console.WriteLine("ContactName = "+ myDataRow["ContactName"]);
Console.WriteLine("Address = "+ myDataRow["Address"]);
}
}
}

The output from this program is as follows:
Retrieving rows from the Customers table
CustomerID = ALFKI
CompanyName = Alfreds Futterkiste
ContactName = Maria Anders

Address = Obere Str. 57
CustomerID = ANATR
CompanyName = Ana Trujillo Emparedados y helados
ContactName = Ana Trujillo
Address = Avda. de la Constitución 2222
CustomerID = ANTON
CompanyName = Antonio Moreno Taquería
ContactName = Antonio Moreno
Address = Mataderos 2312
CustomerID = AROUT
CompanyName = Around the Horn
ContactName = Thomas Hardy
Address = 120 Hanover Sq.
CustomerID = BERGS
CompanyName = Berglunds snabbköp
ContactName = Christina Berglund
Address = Berguvsvägen 8
CustomerID = BLAUS
CompanyName = Blauer See Delikatessen
ContactName = Hanna Moos
Address = Forsterstr. 57
CustomerID = BLONP
CompanyName = Blondesddsl père et fils
ContactName = Frédérique Citeaux
Address = 24, place Kléber
CustomerID = BOLID
CompanyName = Bólido Comidas preparadas
ContactName = Martín Sommer
Address = C/ Araquil, 67
CustomerID = BONAP

CompanyName = Bon app'
ContactName = Laurence Lebihan
Address = 12, rue des Bouchers
CustomerID = BOTTM
CompanyName = Bottom-Dollar Markets
ContactName = Elizabeth Lincoln
Address = 23 Tsawassen Blvd.

×