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

Session 19—Using DataReaders

193

OM

t_bands DER BY

band_title

You will need to create the prBandDelete stored procedure in the Music database in order to get this example to work correctly.

Note

When the ExecuteReader method is called, an OleDbDataReader, oDR, is constructed and then iterated through the data to repopulate the lstBands ListBox. After repopulating lstBands, we inspect to see that the DataReader has been closed, using the isClosed property. We then use the RecordsAffected property to display the number of records that were deleted from t_bands table.

Using DataReader Methods

Now that you know the properties you are likely to use most often, let’s move onto the DataReader’s methods. The DataReader objects provide a plethora of methods.

Read method

We’ve touched on the Read method in an earlier example (Listing 19-2). The Read method advances the DataReader object to the next record each time it is called. In the “Old World” of ADO you would have had to use a combination of several properties and methods, including EOF and MoveNext, to perform the same function as the DataReader’s Read method. Since the DataReader provides for forward-only navigation, the Read method really minimizes the amount of code you need to write to get to your data. We do not provide a Read method example here as you can refer to several of the previous examples to see it in action.

GetValue method

The GetValue method returns the value of a specified field in its native format. You can effectively use the GetValue method in place of the Item property. The GetValue method accepts either an integer representing a column index or a string representing a column name. For example, if the first column (index of 0) of our table is called band_id, we can use the following statement to get its value:

iID = oDR.GetValue(0)

Or, we can use the following:

iID = oDR.GetValue(“band_id”)

Since band_id is set as an integer in our table, the value returned from the GetValue method will return an integer, its native format.

194

Saturday Evening

Get[Data Type] methods

The DataReader object provides a multitude of what we call the Get[Data Type] methods, including the GetString, GetInt32, and GetBoolean methods. The Get[Data Type] methods return the data in a column as the specified data type. For example, the GetString method will return the data in a column in the form of a string. However, no data type conversion is performed, so the data type of the column must be of the data type specified. Unlike the GetValue method, the Get[Data Type] methods only accept a column index, also called an ordinal reference, as a parameter. The following statements demonstrate the

GetString method:

Dim sBandName As String sBandName = oDR.GetString(0)

GetOrdinal method

The GetOrdinal method returns a column’s ordinal reference value, or index, as an integer when passed a column name. For example, the following code returns 0 because band_id is the first column in the t_bands table:

Dim iOrdinal As Integer

iOrdinal = oDR.GetOrdinal(“band_id”)

GetName method

The GetName method is the exact opposite of the GetOrdinal method. It returns a column’s name as a string when passed its index. For example, the following code snippet will return band_id:

Dim sName As String sName = oDR.GetName(0)

Close method

As the name implies, the Close method closes a DataReader object. Unlike other objects, closing the DataReader object is mandatory when you’re done using it. You will get an error if you don’t close your DataReader and then attempt to alter your Connection object. Closing a DataReader’s Connection object immediately closes the DataReader. The syntax is very simple:

oDR.Close()

That covers the DataReader’s major properties and methods.

Note

There are other DataReader methods and properties. However, chances are you may never use them. For a complete listing, refer to the .NET documentation.