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

214

Sunday Morning

By the way, when you run the previous two examples, the Count property should return a value of zero because you haven’t actually added any DataTable objects to the

DataTableCollection object.

TablesCollection Object

Now that you know what the DataTableCollection object is and how to access it explicitly and via the DataSet object, let’s take a look at its properties and methods.

Count property

Because we have already used the Count property in a previous example, we’ll keep this explanation short and sweet. The Count property, which is read-only, returns the number of DataTable objects in the DataTableCollection object.

Item property

Item is probably the DataTableCollection property you will use most frequently. The Item property gets a specified DataTable from the DataTableCollection. In order to get the desired table you either pass an integer representing the table’s index or a string representing the table’s name to the Item property. Listing 21-1 illustrates how you might use the Item property:

Listing 21-1 Using the Item property

<%@ Page Language=”VB” %>

<%@ Import Namespace=”System.Data” %>

<%@ Import Namespace=”System.Data.OleDb” %> <SCRIPT LANGUAGE=”VB” RUNAT=”server”>

Sub Page_Load(Sender As Object, E As EventArgs) Dim oConn As New OleDbConnection

Dim oCmd As New OleDbCommand Dim oDA As New OleDbDataAdapter Dim oDS As DataSet

Dim i As Integer

Dim x As Integer

With oConn

.ConnectionString = “Provider=SQLOLEDB; Data Source=(local);

Initial Catalog=Music; User ID=music; Password=music”

.Open

End With

With oCmd

.Connection = oConn

.CommandType = CommandType.Text

.CommandText = “SELECT * FROM t_bands”

End With

Session 21—Introducing DataSets, Part II

215

oDS = New DataSet(“Music”)

oDA.SelectCommand = oCmd oDA.Fill(oDS, “t_bands”)

oDA.SelectCommand.CommandText = “SELECT * FROM t_music_types” oDA.Fill(oDS, “t_music_types”)

oDA.SelectCommand.CommandText = “SELECT * FROM t_record_companies” oDA.Fill(oDS, “t_record_companies”)

For i = 0 To oDS.Tables.Count - 1 Response.Write(oDS.Tables.Item(i).TableName & “<BR>” & chr(13)) For x = 0 To oDS.Tables.Item(i).Columns.Count - 1

Response.Write(“    ” & oDS.Tables.Item(i).Columns.Item(x).ColumnName & “<BR>” & chr(13))

Next

Next

oDS.Dispose() oDS = Nothing oDA.Dispose() oDA = Nothing oCmd.Dispose() oCmd = Nothing oConn.Close() oConn = Nothing

End Sub </SCRIPT> <HTML> <BODY> </BODY> </HTML>

For a complete listing of the DataTableCollection’s properties, please refer to the .NET documentation.

Cross-Ref

Contains method

The Contains method returns a Boolean value indicating whether the DataTableCollection and thereby the DataSet contain a specified table. The Contains method accepts, as input, a string representing the table’s name. Here’s a quick example:

Dim bValid As Boolean

bValid = oDS.Tables.Contains(“t_bands”)

Based on previous DataTableCollection examples, bValid should be true.