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

Tài liệu The DataSet Class phần 3 doc

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 (35.47 KB, 9 trang )

The output from this program is as follows:
numberOfRows = 3

Reading from the Products DataTable
ProductID = 1
ProductName = Chai
UnitPrice = 18
ProductID = 2
ProductName = Chang
UnitPrice = 19

Reading from the Customers DataTable
CustomerID = ALFKI
CompanyName = Alfreds Futterkiste
Changing the CommandText Property of the SelectCommand
You can also populate a DataSet with multiple DataTable objects by changing the
CommandText property of the SelectCommand for your DataAdapter object before each
call to the Fill() method. First, the following code populates a DataSet with a DataTable
containing two rows from the Products table:
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT TOP 2 ProductID, ProductName, UnitPrice " +
"FROM Products " +
"ORDER BY ProductID";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
int numberOfRows = mySqlDataAdapter.Fill(myDataSet, "Products");
The myDataSet object now contains a DataTable named Products.
Next, the CommandText property for the SelectCommand of mySqlDataAdapter is


changed to a SELECT statement that retrieves rows from the Customers table, and the
Fill() method is called again:
mySqlDataAdapter.SelectCommand.CommandText =
"SELECT CustomerID, CompanyName " +
"FROM Customers " +
"WHERE CustomerID = 'ALFKI'";
numberOfRows = mySqlDataAdapter.Fill(myDataSet, "Customers");
mySqlConnection.Close();
The myDataSet object now contains an additional DataTable named Customers.
Listing 10.6
shows a program that uses the code examples shown in this section.
Listing 10.6: MULTIPLEDATATABLES2.CS

/*
MutlipleDataTables2.cs illustrates how to populate a DataSet
object with multiple DataTable objects by changing the
CommandText property of a DataAdapter object's SelectCommand
*/

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

class MultipleDataTables2
{
public static void Main()
{
SqlConnection mySqlConnection =
new SqlConnection(
"server=localhost;database=Northwind;uid=sa;pwd=sa"

);

SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT TOP 2 ProductID, ProductName, UnitPrice " +
"FROM Products " +
"ORDER BY ProductID";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
int numberOfRows = mySqlDataAdapter.Fill(myDataSet, "Products");
Console.WriteLine("numberOfRows = " + numberOfRows);

// change the CommandText property of the SelectCommand
mySqlDataAdapter.SelectCommand.CommandText =
"SELECT CustomerID, CompanyName " +
"FROM Customers " +
"WHERE CustomerID = 'ALFKI'";
numberOfRows = mySqlDataAdapter.Fill(myDataSet, "Customers");
Console.WriteLine("numberOfRows = " + numberOfRows);

mySqlConnection.Close();

foreach (DataTable myDataTable in myDataSet.Tables) {
Console.WriteLine("\nReading from the " +
myDataTable.TableName + "DataTable");
foreach (DataRow myDataRow in myDataTable.Rows)
{
foreach (DataColumn myDataColumn in myDataTable.Columns)

{
Console.WriteLine(myDataColumn + "= " +
myDataRow[myDataColumn]);
}
}
}
}
}


The output from this program is as follows:
numberOfRows = 2
numberOfRows = 1

Reading from the Products DataTable
ProductID = 1
ProductName = Chai
UnitPrice = 18
ProductID = 2
ProductName = Chang
UnitPrice = 19

Reading from the Customers DataTable
CustomerID = ALFKI
CompanyName = Alfreds Futterkiste
Using Multiple DataAdapter Objects to Populate the Same DataSet Object
You can also populate the same DataSet with multiple DataTable objects using different
DataAdapter objects. For example, assume you already have a DataSet named myDataSet
that was populated using a SqlDataAdapter named mySqlDataAdapter, and that
myDataSet currently contains a DataTable named Products. The following example

creates another SqlDataAdapter and uses it to populate myDataSet with another
DataTable named Customers:
SqlDataAdapter mySqlDataAdapter2 = new SqlDataAdapter();
mySqlDataAdapter2.SelectCommand = mySqlCommand;
mySqlDataAdapter2.SelectCommand.CommandText =
"SELECT CustomerID, CompanyName " +
"FROM Customers " +
"WHERE CustomerID = 'ALFKI'";
numberOfRows = mySqlDataAdapter2.Fill(myDataSet, "Customers");
Listing 10.7
shows a program that uses the code examples shown in this section.
Listing 10.7: MULTIPLEDATATABLES3.CS

/*
MutlipleDataTables3.cs illustrates how to populate a DataSet
object with multiple DataTable objects using multiple
DataAdapter objects to populate the same DataSet object
*/

using System;
using System.Data;
using System.Data.SqlClient;
class MultipleDataTables3
{
public static void Main()
{
SqlConnection mySqlConnection =
new SqlConnection(
"server=localhost;database=Northwind;uid=sa;pwd=sa"
);


SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT TOP 2 ProductID, ProductName, UnitPrice " +
"FROM Products " +
"ORDER BY ProductID";
SqlDataAdapter mySqlDataAdapter1 = new SqlDataAdapter();
mySqlDataAdapter1.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
int numberOfRows = mySqlDataAdapter1.Fill(myDataSet, "Products");
Console.WriteLine("numberOfRows = " + numberOfRows);

// create another DataAdapter object
SqlDataAdapter mySqlDataAdapter2 = new SqlDataAdapter();
mySqlDataAdapter2.SelectCommand = mySqlCommand;
mySqlDataAdapter2.SelectCommand.CommandText =
"SELECT CustomerID, CompanyName " +
"FROM Customers " +
"WHERE CustomerID = 'ALFKI'";
numberOfRows = mySqlDataAdapter2.Fill(myDataSet, "Customers");
Console.WriteLine("numberOfRows = " + numberOfRows);
mySqlConnection.Close();

foreach (DataTable myDataTable in myDataSet.Tables) {
Console.WriteLine("\nReading from the " +
myDataTable.TableName + "DataTable");
foreach (DataRow myDataRow in myDataTable.Rows)
{
foreach (DataColumn myDataColumn in myDataTable.Columns)

{
Console.WriteLine(myDataColumn + "= " +
myDataRow[myDataColumn]);
}
}
}
}
}


The output from this program is as follows:
numberOfRows = 2
numberOfRows = 1

Reading from the Products DataTable
ProductID = 1
ProductName = Chai
UnitPrice = 18
ProductID = 2
ProductName = Chang
UnitPrice = 19

Reading from the Customers DataTable
CustomerID = ALFKI
CompanyName = Alfreds Futterkiste
Merging DataRow, DataSet, and DataTable Objects into Another DataSet
In this section, you'll learn how to use the Merge() method to merge DataRow, DataSet,
and DataTable objects into another DataSet. You might want to do this when you have
multiple sources of data; for example, you might get data from many regional offices that
is sent to headquarters, and you need to merge all that data into one DataSet.

The Merge() method is overloaded as follows:
void Merge(DataRow[] myDataRows)
void Merge(DataSet myDataSet)
void Merge(DataTable myDataTable)
void Merge(DataSet myDataSet, bool preserveChanges)
void Merge(DataRow[] myDataRows, bool preserveChanges,
MissingSchemaAction myMissingSchemaAction)
void Merge(DataSet myDataSet, bool preserveChanges,
MissingSchemaAction myMissingSchemaAction)
void Merge(DataTable myDataTable, bool preserveChanges,
MissingSchemaAction myMissingSchemaAction)
where
• PreserveChanges specifies whether changes in the current DataSet (the DataSet
with the Merge() method that is called) are to be kept.
• MyMissingSchemaAction specifies the action to take when the current DataSet
doesn't have the same tables or columns as the DataRow, DataSet, or DataTable
being merged into that DataSet.
You set myMissingSchemaAction to one of the constants defined in the
System.Data.MissingSchemaAction enumeration. Table 10.7
shows the constants defined
in the MissingSchemaAction enumeration.
Table 10.7: MissingSchemaAction ENUMERATION MEMBERS
CONSTANT DESCRIPTION
Add The column or table is added to the current DataSet. Add is the default.
Table 10.7: MissingSchemaAction ENUMERATION MEMBERS
CONSTANT DESCRIPTION
AddWithKey The column and primary key information is added to the current DataSet.
Error A SystemException is thrown.
Ignore The column or table is ignored and not read.
Listing 10.8 illustrates the use of the Merge() method.

Listing 10.8: MERGE.CS

/*
Merge.cs illustrates how to use the Merge() method
*/

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

class Merge
{
public static void Main()
{
SqlConnection mySqlConnection =
new SqlConnection(
"server=localhost;database=Northwind;uid=sa;pwd=sa"
);

SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

// populate myDataSet with three rows from the Customers table
mySqlCommand.CommandText =
"SELECT CustomerID, CompanyName, ContactName, Address " +
"FROM Customers " +
"WHERE CustomerID IN ('ALFKI', 'ANATR', 'ANTON')";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();

mySqlDataAdapter.Fill(myDataSet, "Customers");

// populate myDataSet2 with two rows from the Customers table
mySqlCommand.CommandText =
"SELECT CustomerID, CompanyName, ContactName, Address " +
"FROM Customers " +
"WHERE CustomerID IN ('AROUT', 'BERGS')";
DataSet myDataSet2 = new DataSet();
mySqlDataAdapter.Fill(myDataSet2, "Customers2");


// populate myDataSet3 with five rows from the Products table
mySqlCommand.CommandText =
"SELECT TOP 5 ProductID, ProductName, UnitPrice " +
"FROM Products " +
"ORDER BY ProductID";
DataSet myDataSet3 = new DataSet();
mySqlDataAdapter.Fill(myDataSet3, "Products");

mySqlConnection.Close();

// merge myDataSet2 into myDataSet
myDataSet.Merge(myDataSet2);

// merge myDataSet3 into myDataSet
myDataSet.Merge(myDataSet3, true, MissingSchemaAction.Add);

// display the rows in myDataSet
foreach (DataTable myDataTable in myDataSet.Tables)
{

Console.WriteLine("\nReading from the " + myDataTable + "DataTable");
foreach (DataRow myDataRow in myDataTable.Rows)
{
foreach (DataColumn myDataColumn in myDataTable.Columns)
{
Console.WriteLine(myDataColumn + "= " +
myDataRow[myDataColumn]);
}
}
}
}
}

The output from this program is as follows:
Reading from the Customers DataTable
CustomerID = ALFKI
CompanyName = Alfreds Futterkiste
ContactName = Maria Anders
Address = Obere Str. 57
CustomerID = ANATR
CompanyName = Ana Trujillo3 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

Reading from the Customers2 DataTable
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

Reading from the Products DataTable
ProductID = 1
ProductName = Chai
UnitPrice = 18
ProductID = 2
ProductName = Chang
UnitPrice = 19
ProductID = 3
ProductName = Aniseed Syrup
UnitPrice = 10
ProductID = 4
ProductName = Chef Anton's Cajun Seasoning
UnitPrice = 22
ProductID = 5
ProductName = Chef Anton's Gumbo Mix
UnitPrice = 21.35

×