Đang chuẩn bị liên kết để tải về tài liệu:
The DataSet Class phần 2

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Note The Fill() method will actually open and close the Connection for you if you don't open it first, however, it is better to explicitly open and close the Connection because that way it is clearer what your program is doing. | Note The Fill method will actually open and close the Connection for you if you don t open it first however it is better to explicitly open and close the Connection because that way it is clearer what your program is doing. Also if you re calling the Fill method repeatedly over a short span of code you ll want to keep the database connection open and close it only when you re finished. The DataSet is now populated with a DataTable named Products. You can read the Products DataTable from myDataSet using the following example DataTable myDataTable myDataSet.Tables Products You can also read the Products DataTable using an int value DataTable myDataTable myDataSet.Tables 0 You can display the column values for each row in myDataTable using the following foreach loop that iterates over the DataRow objects stored in myDataTable notice the use of the myDataTable object s Rows property foreach DataRow myDataRow in myDataTable.Rows Console.WriteLine ProductID myDataRow ProductID Console.WriteLine ProductName myDataRow ProductName Console.WriteLine UnitPrice myDataRow UnitPrice The Rows property returns a DataRowCollection object that allows you to access all the DataRow objects stored in myDataTable. You can read each column value in a DataRow using the name of the column for example to read the ProductID column value you use myDataRow ProductID . You can also use the numeric position of the column for example myDataRow 0 returns the value for the first column. This is the ProductID column. You can also use the following code to iterate over all the DataTable DataRow and DataColumn objects stored in myDataSet foreach DataTable myDataTable in myDataSet.Tables foreach DataRow myDataRow in myDataTable.Rows foreach DataColumn myDataColumn in myDataTable.Columns Console.WriteLine myDataColumn myDataRow myDataColumn Notice you don t need to know the names of the DataTable or DataColumn objects to display them. The call to the WriteLine method displays myDataColumn which returns