tailieunhanh - The DataSet Class phần 2
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 Products You can also read the Products DataTable using an int value DataTable myDataTable 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 ProductID myDataRow ProductID ProductName myDataRow ProductName 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 foreach DataRow myDataRow in foreach DataColumn myDataColumn in 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
đang nạp các trang xem trước