Reading Rows from a SqlDataReader Object

Reading Rows from a SqlDataReader Object

You read the rows from a DataReader object using the Read() method. This method returns the Boolean true value when there is another row to read, otherwise it returns false.

You can read an individual column value in a row from a DataReader by passing the name of the column in square brackets. For example, to read the CustomerID column, you use productsSqlDataReader["ProductID"]. You can also specify the column you want to get by passing a numeric value in brackets. For example, productsSqlDataReader[0] also returns the ProductID column value.

Tip 

The difference between these two ways of reading a column value is performance: using numeric column positions instead of column names results in faster execution of the code.

Let's take a look at two code snippets that illustrate these two ways of reading column values. The first code snippet uses the column names to read the column values:

 while (productsSqlDataReader.Read()) {   Console.WriteLine(productsSqlDataReader["ProductID"]);   Console.WriteLine(productsSqlDataReader["ProductName"]);   Console.WriteLine(productsSqlDataReader["UnitPrice"]);   Console.WriteLine(productsSqlDataReader["Discontinued"]); } 

The second code snippet uses the numeric column positions to read the column values:

 while (productsSqlDataReader.Read()) {   Console.WriteLine(productsSqlDataReader[0]);   Console.WriteLine(productsSqlDataReader[1]);   Console.WriteLine(productsSqlDataReader[2]);   Console.WriteLine(productsSqlDataReader[3]); } 

Although the second code snippet is faster, it is less flexible since you have to hard-code the numeric column positions. If the column positions in the SELECT statement are changed, you need to change the hard-coded column positions in the code-and this is a maintenance nightmare. Also, hard-coding the column positions makes your programs more difficult to read.

There is a solution to this problem: you can call the GetOrdinal() method of your DataReader object. The GetOrdinal() method returns the position of a column given its name; this position is known as the column's ordinal. You can then use the position returned by GetOrdinal() to get the column values from your DataReader.

Let's take a look at some code that uses the GetOrdinal() method to obtain the positions of the columns from the example SELECT statement:

 int productIDColPos =   productsSqlDataReader.GetOrdinal("ProductID"); int productNameColPos =   productsSqlDataReader.GetOrdinal("ProductName"); int unitPriceColPos =   productsSqlDataReader.GetOrdinal("UnitPrice"); int discontinuedColPos =   productsSqlDataReader.GetOrdinal("Discontinued"); 

You can then use these int values to get the column values from productsSqlDataReader:

 while (productsSqlDataReader.Read()) {   Console.WriteLine(productsSqlDataReader[productIDColPos]);   Console.WriteLine(productsSqlDataReader[productNameColPos]);   Console.WriteLine(productsSqlDataReader[unitPriceColPos]);   Console.WriteLine(productsSqlDataReader[discontinuedColPos]); } 

This way gives you the best of both worlds: high performance and flexibility.

Warning 

When you've finished reading the rows from your DataReader object, close it using the Close() method. The reason for this is that a DataReader object ties up the Connection object, and no other commands can be executed while there is an open DataReader for that Connection.

The following example closes productsSqlDataReader using the Close() method:

 productsSqlDataReader.Close(); 

Once you've closed your DataReader, you can execute other commands using your Connection object.

Listing 9.1 uses the code examples shown in this section.

Listing 9.1: USINGCOLUMNORDINALS.CS

start example
 /*   UsingColumnOrdinals.cs illustrates how to use the GetOrdinal()   method of a DataReader object to get the numeric positions of   a column */ using System; using System.Data; using System.Data.SqlClient; class UsingColumnOrdinals {   public static void Main()   {     SqlConnection mySqlConnection =       new SqlConnection(         "server=localhost;database=Northwind;uid=sa;pwd=sa"       );     SqlCommand mySqlCommand = mySqlConnection.CreateCommand();     mySqlCommand.CommandText =       "SELECT TOP 5 ProductID, ProductName, UnitPrice, " +       "UnitsInStock, Discontinued " +       "FROM Products " +       "ORDER BY ProductID";     mySqlConnection.Open();     SqlDataReader productsSqlDataReader =       mySqlCommand.ExecuteReader();     // use the GetOrdinal() method of the DataReader object     // to obtain the numeric positions of the columns     int productIDColPos =       productsSqlDataReader.GetOrdinal("ProductID");     int productNameColPos =       productsSqlDataReader.GetOrdinal("ProductName");     int unitPriceColPos =       productsSqlDataReader.GetOrdinal("UnitPrice");     int unitsInStockColPos =       productsSqlDataReader.GetOrdinal("UnitsInStock");     int discontinuedColPos =       productsSqlDataReader.GetOrdinal("Discontinued");     while (productsSqlDataReader.Read())     {       Console.WriteLine("ProductID = " +         productsSqlDataReader[productIDColPos]);       Console.WriteLine("ProductName = " +         productsSqlDataReader[productNameColPos]);       Console.WriteLine("UnitPrice = " +         productsSqlDataReader[unitPriceColPos]);       Console.WriteLine("UnitsInStock = " +         productsSqlDataReader[unitsInStockColPos]);       Console.WriteLine("Discontinued = " +         productsSqlDataReader[discontinuedColPos]);     }     productsSqlDataReader.Close();     mySqlConnection.Close();   } } 
end example

The output from this program is as follows:

 ProductID = 1 ProductName = Chai UnitPrice = 18 UnitsInStock = 39 Discontinued = False ProductID = 2 ProductName = Chang UnitPrice = 19 UnitsInStock = 17 Discontinued = False ProductID = 3 ProductName = Aniseed Syrup UnitPrice = 10 UnitsInStock = 13 Discontinued = False ProductID = 4 ProductName = Chef Anton's Cajun Seasoning UnitPrice = 22 UnitsInStock = 53 Discontinued = False ProductID = 5 ProductName = Chef Anton's Gumbo Mix UnitPrice = 21.35 UnitsInStock = 0 Discontinued = True 




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net