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

S E S S I O N

21

Introducing DataSets, Part II

Session Checklist

Learning to construct a DataSet without a DataAdapter object

Learning to navigate the DataSet’s object model

Understanding the relationship between DataSet, DataTable, DataRow, and DataColumn objects

In the previous session, we began our discussion of DataSet objects, the cornerstone of ADO.NET. You learned that a dataset is effectively a disconnected copy of a database and that you can populate a DataSet using a DataAdapter object. In this session,

you’re going to attack some of the DataSet object’s constituent, or child, objects, including the DataTable, DataColumn, and DataRow objects.

Constructing a DataSet

Before we get started with its constituent objects, lets step back for a moment and discuss how to construct a DataSet object. In Session 20, “Introducing DataSets, Part I,” you learned how to construct a DataSet with a DataAdapter object. Oddly enough, you don’t actually need a DataAdapter object to create a DataSet object. Creating a DataSet object is fairly straightforward as shown in the following example:

Dim oDS As DataSet

oDS = New DataSet()

There’s another way to explicitly create a DataSet and that is by passing a name for the DataSet into the constructor as follows:

Dim oDS As DataSet

oDS = New DataSet(“MyDataSet”)

212

Sunday Morning

In Session 20, you learned to set the DataSet’s name using the DataSetName property. The following example does the same thing as passing the name of the DataSet into the constructor:

Dim oDS As DataSet

oDS = New DataSet() oDS.DataSetName = “MyDataSet”

Tables property

As you might suspect, the DataSet object is actually a container class. So what does a

DataSet contain? Well, many things, but most importantly a collection of DataTable objects in the form of a DataTableCollection object. In order to access a DataSet’s DataTable objects, we need to go through the DataSet’s Tables property. Make sense? Figure 21-1 helps you visualize the relationships among all the objects.

As you can see in Figure 21-1, the DataSet is a hierarchy of containers, collections, and objects.

DataSet(Container Object)

DataTableCollection (Collection Object)

DataTable(ContainerObject)

DataColumnCollection (Collection Object)

DataColumn (Object)

DataRowCollection (Collection Object)

DataRow (Object)

ConstraintCollection (Collection Object)

Constraint (Object)

DataRelationCollection (Collection Object)

DataRelation(Object)

Figure 21-1 A DataSet’s object hierarchy

Session 21—Introducing DataSets, Part II

213

A DataSet can contain one or more DataTable objects (among other things) in the form of a DataTableCollection object and, in turn, a DataTable object can contain one or more DataColumn and DataRow objects. So, when you access a DataSet’s Tables property, you are actually accessing a DataTableCollection object. Try this out:

<%@ 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 oDS As DataSet

Dim oDTC As DataTableCollection

oDS = New DataSet(“MyDataSet”) oDTC = oDS.Tables

lblTableCount.Text = oDTC.Count End Sub

</SCRIPT>

<HTML>

<BODY>

MyDataSet contains <asp:Label Id=”lblTableCount” Text=”” Runat=”server” /> tables.

</BODY>

</HTML>

This example illustrates our “Hierarchy Theory.” As you can see, you created a DataSet and called it “MyDataSet” with the following line:

oDS = New DataSet(“MyDataSet”)

You also created a DataTableCollection object and initialized it with the DataSet’s Tables property as follows:

oTS = oDS.Tables

Now that you have a reference to the DataSet’s DataTablesCollection object, you can access its properties and methods. In this example, you simply write out the number of tables in the DataTableCollection object using its Count property as follows:

lblTableCount.Text = oDTC.Count

You actually could have accomplished this using the few lines of code that follow:

Dim oDS As DataSet

oDS = New DataSet(“MyDataSet”) lblTableCount.Text = oDs.Tables.Count

As you can see, even though you are using a DataTableCollection object’s properties and methods, you don’t necessarily need to explicitly create a DataTableCollection object. You can just go though the DataSet object.