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

Session 18—Executing Commands

183

You’ll notice in Listing 18-5 that we first created the OleDbCommand, oCmd, object. Next, we constructed the OleDbParameter, oParam, object, and set its properties. Finally, we attached the OledbParameter to the OleDbCommand object using the Parameters collection’s Add () method with the following line of code:

oCmd.Parameters.Add(oParam)

The Parameter object supports many properties and methods and can become very complex. You should check your .NET documentation for a more detailed look at the

OleDbParameter and SqlParameter objects.

Executing a Command

Now that you know how to construct an OLEDBCommand object, it is time that you ask it do something. The OLEDBCommand object has many useful methods, including the

ExecuteReader(), ExecuteNonQuery(), and Prepare() methods.

ExecuteNonQuery method

The ExecuteReader() and ExecuteNonQuery() methods are similar in that they both execute commands against a data source. The main difference is the number of rows returned when the command is executed. As indicated by its name, the ExecuteNonQuery() method does not return any rows from the datasource; you probably won’t use this command when executing a SQL SELECT command. It could, however, be useful when executing INSERT, UPDATE or DELETE commands depending on your requirements. The ExecuteNonQuery() method does not require, or for that matter accept any parameters in its constructor. Here is a sample of calling the ExecuteNonQuery method:

oCmd = New OleDbCommand() oCmd.Connection = oConn oCmd.CommandType = CommandType.Text

oCmd.CommandText = “UPDATE t_bands SET band_title = ‘Hootie and The Blowfish’ WHERE band_title = ‘Hootie & The Blowfish’”

oCmd.ExecuteNonQuery()

You’ll notice that we are executing a SQL UPDATE command so we probably don’t want any records returned. The ExecuteNonQuery() method does return the number of rows, as an integer, that were affected by the executed command. So if you wanted to determine how many records were affected by a command, you could use the following code:

Dim iAffected As Integer iAffected = oCmd.ExecuteNonQuery()

Prepare method

The Prepare() method is used to create a prepared, or compiled, version of the command on the datasource. This method is generally used only when the CommandType property is set to Text; but it does improve performance when executing large SQL commands or

184

Saturday Evening

dynamically generated SQL commands that contain parameters. The syntax for preparing a command is

oCmd.Prepare()

ExecuteReader method

The ExecuteReader() method executes the CommandText against the command’s Connection and builds an object capable of forward-only data reads. This object is an OleDbDataReader. The syntax is

oDR = oCmd.ExecuteReader()

where oDR is an OleDbDataReader object. Simple! Once you have populated the

OleDbDataReader object by calling the OleDbCommand’s ExecuteReader() method, you have access to the data. We cover DataReader objects in detail in Session 19, “Using DataReaders.” Listing 18-5 demonstrates how to populate a datareader via the command object’s Execute method.

Listing 18-5 Populating a DataReader with a command

<%@ 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.StoredProcedure

.CommandText = “prGetBands” oDR = oCmd.ExecuteReader()

End With

While oDR.Read()

lstBands.Items.Add(New

ListItem(oDR.Item(“band_title”),oDR.Item(“band_id”)))

End While

oDR.Close()

oConn.Close() End Sub