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

Session 20—Introducing DataSets, Part I

199

Constructing a DataAdapter Object

As with many ADO.NET objects, there are several ways to construct a DataAdapter object. The default constructor is as follows:

oDA = New OleDbDataAdapter()

You’ll notice that no parameters are passed using the default method. The DataAdapter’s properties need to be set explicitly after construction. With the second method, we construct a DataAdapter with a specified select OleDbCommand object as follows:

oDA = New OleDbDataAdpater(oCmd)

The third method initializes a new instance of a DataAdapter with a select command string (for example a SQL statement) and a valid OleDbConnection object as follows:

oDA = New OleDbDataAdapter(sSQL, oConn)

Finally, we can initialize an OleDbDataAdapter with a select command string and a connection string as follows:

oDSComd = New OLEDBDataAdapter(sSQL, sConnString)

This method of constructing a DataAdapter object differs from the previous because a connection to the database specified in the connection string is created when the DataAdapter is executed.

In the following snippet of code, we demonstrate initializing a DataAdapter object by passing a command string and an OleDbConnection object:

<%@ 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 OleDbConnection

Dim oDA As OleDbDataAdapter

oConn = New OleDbConnection(“Provider=SQLOLEDB;Data

Source=(local);Initial Catalog=Music;User ID=music;Password=music”)

oConn.Open

oDA = New OleDbDataAdapter(“SELECT * FROM t_bands”, oConn) oDA = Nothing

oConn.Close() oConn = Nothing

End Sub </SCRIPT>

The DataSet object provides several very useful properties, most of which revolve around manipulating DataSet content.

200

Saturday Evening

SelectCommand property

The SelectCommand property gets or sets a Command object used to select records in a DataSet. In following code snippet, we will create an OleDbDataAdapter object and set the

SelectCommand:

<%@ 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 OleDbConnection

Dim oCmd As OleDbCommand Dim oDA As OleDbDataAdapter

oConn = New OleDbConnection(“Provider=SQLOLEDB;Data

Source=(local);Initial Catalog=Music;User ID=music;Password=music”)

oConn.Open()

oCmd = New OleDbCommand(“SELECT * FROM t_bands”, oConn)

oDA = New OleDbDataAdapter() oDA.SelectCommand = oCmd

End Sub </SCRIPT>

You notice in the previous example that we (1) create an OleDbConnection object, (2) create an OleDbCommand object, (3) construct an OleDbDataAdapter object, and (4) set the

OleDbDataAdapter object’s SelectCommand equal to the previously created OleDbCommand object.

When the SelectCommand property is set to a previously created Command object, as in the previous example, the Command object is not cloned. The SelectCommand merely maintains a reference to the Command object — as shown in the following example. We set the SelectCommand property without explicitly creating a Command object:

<%@ 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 oDA As New OleDbDataAdapter

With oConn

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

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

.Open

End With

Session 20—Introducing DataSets, Part I

201

oDA.SelectCommand = New OleDbCommand With oDA.SelectCommand

.CommandType = CommandType.Text

.CommandText = “SELECT * FROM t_bands”

.Connection = oConn End With

oDA = Nothing oConn.Close oConn = Nothing

End Sub </SCRIPT>

Make sense? In effect, we’re creating the SelectCommand explicitly.

UpdateCommand, DeleteCommand, and InsertCommand properties

The UpdateCommand property is used to get or set the command used to update records in the data source. The UpdateCommand is effectively the Command object used to update records in the data source for modified rows in the DataSet. When a DataAdapter object’s Update method is called and (1) the UpdateCommand property is not set and (2) primary key information is present in the DataSet, the UpdateCommand will be generated automatically.

To keep things brief here, just say that the DeleteCommand and InsertCommand properties are used to get or set the command used to delete or insert, respectively, records in the data source when the Update method is called.

We’ll return to the DataAdapter properties later. But first, take a look at the methods you can use to create DataSet objects.

Fill method

The Fill method is probably the DataAdapter method you will use most frequently. Simply stated, the Fill method adds data from your data source to a dataset. The Fill method accepts a variety of parameters including the DataSet object to fill, a string representing the alias for the newly created DataSet object, an integer representing the lower bound of records to retrieve, and an integer representing the upper bound of records to retrieve from our data source. Here are some examples:

oDSCmd.Fill(oDS)

oDSCmd.Fill(oDS, “Band Information”)

In the previous sample, the only parameter that is required is the DataSet. Listing 20-1 details how to create a DataSet called “Band Information” and bind it to a DataGrid, dgBands, control.

202

Saturday Evening

Listing 20-1 Creating a DataSet and binding it to a DataGrid control

<%@ 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 OleDbConnection

Dim oDA As OleDbDataAdapter Dim oDS As New DataSet

oConn = New OleDbConnection(“Provider=SQLOLEDB;Data

Source=(local);Initial Catalog=Music;User ID=music;Password=music”)

oConn.Open

oDA = New OleDbDataAdapter(“SELECT * FROM t_bands”, oConn) oDA.Fill(oDS, “Band Information”)

dgBands.DataSource = oDS dgBands.DataBind()

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

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

<asp:DataGrid id=”dgBands” runat=”server” BorderColor=”#000000” BorderWidth=”2”

GridLines=”Both”

CellPadding=”5”

CellSpacing=”0”

Font-Name=”Arial” HeaderStyle-BackColor=”#C0C0C0”

/>

</BODY>

</HTML>

As you can see, we simply create and open a connection to the Music database, create a DataAdapter object, and fill the DataSet by calling the Fill method. If you run this example, you may be a little surprised by the output. Instead of a list of bands from the t_bands table, you actually get a list of the tables in the DataSet. That is because in the process of binding the DataSet, oDS, to the DataGrid control, dgBands, we set the DataGrids DataSource property as follows:

dgBands.DataSource = oDS