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

Session 18—Executing Commands

185

</SCRIPT>

<HTML>

<BODY>

<asp:ListBox ID=”lstBands” Size=”1” Runat=”server” /> </BODY>

</HTML>

In Listing 18-6, we use the following line of code to populate the DataReader object with the results of the query:

oDR = oCmd.ExecureReader()

Once the DataReader is populated, we iterate through the DataReader and add an Item to the ListBox server control, lstBands, for each record using the following statement:

lstBands.Items.Add(New ListItem(oDR.Item(“band_title”),oDR.Item(“band_id”)))

This method of populating controls with data is very fast, but not very flexible as the DataReader objects provide for forward-only navigation.

REVIEW

The ADO.NET Command objects, OleDbCommand and SqlCommand, are used to execute commands against a data source. With the Command objects you can execute SQL statements to insert, update, delete, or select data as well as create database objects such as stored procedures, tables, or triggers. Additionally, using the command object’s ExecuteReader() method, we can fill a DataReader object for forward-only navigation.

QUIZ YOURSELF

1.What is the main function of a Command object? (See session introduction.)

2.What Command property do you use to add parameters to a Command object? (See “Appending parameters.”)

3.What are the differences between the ExecuteReader () and ExecuteNonQuery () methods? (See “ExecuteNonQuery method.”)

S E S S I O N

19

Using DataReaders

Session Checklist

Understanding the function of the DataReader objects in ADO.NET

Filling a DataReader using the Command object’s Execute method

Moving through a DataReader

In the previous session, we introduced the ADO.NET Command objects. In this session, we build upon the previous discussion of Command objects and further discuss the ADO.NET DataReader objects. As usual, ADO.NET offers two flavors of DataReader objects:

OleDbDataReader and SqlDataReader. Just like the Connection and Command objects, you can use either to access a SQL Server database, but need to use the OleDbDataReader object to access any other data source.

Introducing DataReaders

So, what is a DataReader? A DataReader object is effectively a forward-only collection of records from your data source. The interesting thing about DataReaders is that they do not have a public constructor per se. The DataReader is created via a Command object’s ExecuteReader method. Another interesting thing to note about DataReader objects is that, unlike many other ADO.NET objects, they can’t be disconnected — that is, they always need an active connection. Thus, you can’t, for example, pass them between business objects. The purpose of the DataReader is to provide data for display, that’s it. The DataReader objects are lightweight and very fast so they are ideal for this purpose.

Looked at in terms of our telephone analogy, the DataReader is analogous to a recording you might get when say calling a restaurant and requesting directions. When you’re listening to the directions and feverishly trying to write everything down you might often miss something. The problem is, you can’t go back because the recording is forward-only. I guess you could call back! Same with a DataReader. It’s forward only!

188

Saturday Evening

When creating a DataReader, start by declaring a variable as follows:

Dim oDR As OleDbDataReader

The next thing you need to do is construct your Connection and Command objects.

For more details on constructing Connection and Command objects, refer to Sessions 17 and 18.

Cross-Ref

Next, initialize the DataReader object by calling the Command object’s ExecuteReader method as follows:

oDR = oCmd.Execute()

Now that is easy! Let’s bring it all together . . . The following example illustrates how to

(1) construct and open a Connection, (2) construct a Command, and (3) call the Command’s ExecuteReader method and pass the result to a DataReader, as shown in Listing 19-1.

Listing 19-1 Constructing a DataReader

<%@ 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 oDR As OleDbDataReader

oConn = New OleDbConnection(“Provider=SQLOLEDB;Data

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

oConn.Open()

oCmd = New OleDbCommand() With oCmd

.Connection = oConn

.CommandType = CommandType.Text

.CommandText = “SELECT * FROM t_bands” oDR = .ExecuteReader()

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

Creating a DataReader with ADO.NET </BODY>

</HTML>