Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

Session 21—Introducing DataSets, Part II

221

As you can see in Listing 21-2, you have constructed a DataSet object , oDS, using a OleDbDataAdapter object. You then created a reference to the t_bands table, oDT, through the DataSet’s Tables property. Then you iterated through the columns in the t_bands table using a For . . . Next structure and added a header cell to the .NET TableRow object, oTR. The For structure is basically saying “For each column in t_bands, add a cell to the row.” When you were done adding header cells, you added the row to the .NET Table control, tblBands. After you added your table header, you began iterating through the DataRow objects in oDT using another For . . . Next structure. For each row in t_bands, you then iterate through the columns and add a cell to a TableRow object as follows:

For Each oDC In oDT.Columns

oTC = New TableCell oTC.Text = oDR.Item(oDC) oTR.Cells.Add(oTC)

Next

The key piece of code here is the line in which we obtain the data in our column using the syntax oDR.Item(oDC). All this is saying is “for this row, give me the data in column oDC.” Pretty simple once you get the hang of it. Although you could have accomplished the same thing, with a lot less code, using DataBinding, this example illustrates some important concepts.

OK, those are all the properties we are going to cover for the DataTable object. The DataTable object also provides numerous methods, but to be quite honest you probably won’t use most of them. We’ll address only two of the methods, Dispose and NewRow, provided by the DataTable object. If you’re interested in more, take a gander at your

.NET documentation for a complete listing.

Dispose method

As usual it is good programming practice to dispose of all your objects after you are done using them in order to release valuable system resources. The DataTable object’s Dispose method does this for you. Refer to Listing 21-2, to see the Dispose method in action.

(Although there isn’t too much to see.)

NewRow method

The NewRow method creates a new DataRow object with the same schema as the table through which it is being created. Once a row is created, you can add it to the table’s DataRowCollection via the Rows property. Here’s an example:

Dim oDR As DataRow

oDR = oDS.Tables(0).NewRow oDR(“band_title”) = “Toad The Wet Sprocket” ‘ [ADD OTHER COLUMN INFORMATION HERE] oDS.Tables(0).Rows.Add(oDR)

This example creates a new row in the first table (index of zero) in our DataSet, oDS.