Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Creating and Using a DataRelation Object doc
MIỄN PHÍ
Số trang
7
Kích thước
27.8 KB
Định dạng
PDF
Lượt xem
840

Tài liệu Creating and Using a DataRelation Object doc

Nội dung xem thử

Mô tả chi tiết

Creating and Using a DataRelation Object

In this section, you'll learn how to create a DataRelation object to define a relationship

between two DataTable objects that hold some rows from the Customers and Orders

tables. As you know, the CustomerID column of the child Orders table is a foreign key

that links to the CustomerID column of the parent Customers table.

Once you've created a DataRelation, you can use the GetChildRows() method of a

DataRow object in the parent DataTable to obtain the corresponding DataRow objects

from the child DataTable. By "corresponding," I mean the rows that have matching

values in the foreign key DataColumn objects. You can also use the GetParentRow()

method of a DataRow in the child DataTable to obtain the corresponding DataRow in the

parent DataTable.

Before creating and adding a DataRelation to a DataSet, you first need a DataSet. The

following example creates and populates a DataSet with two DataTable objects named

customersDT and ordersDT; notice that the top two rows from the Customers table along

with the corresponding rows from the Orders table are retrieved:

SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

mySqlCommand.CommandText =

"SELECT TOP 2 CustomerID, CompanyName " +

"FROM Customers " +

"ORDER BY CustomerID;" +

"SELECT OrderID, CustomerID " +

"FROM Orders " +

"WHERE CustomerID IN (" +

" SELECT TOP 2 CustomerID " +

" FROM Customers " +

" ORDER BY CustomerID" +

")";

SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

mySqlDataAdapter.SelectCommand = mySqlCommand;

DataSet myDataSet = new DataSet();

mySqlConnection.Open();

mySqlDataAdapter.Fill(myDataSet);

mySqlConnection.Close();

myDataSet.Tables["Table"].TableName = "Customers";

myDataSet.Tables["Table1"].TableName = "Orders";

DataTable customersDT = myDataSet.Tables["Customers"];

DataTable ordersDT = myDataSet.Tables["Orders"];

Tải ngay đi em, còn do dự, trời tối mất!