[ Team LiB ]Recipe 9.7 Improving DataReader Performance with Column Ordinals Problem You want to use column ordinals rather than column names to retrieve data from a DataReader to impr
Trang 1[ Team LiB ]
Recipe 9.7 Improving DataReader Performance with Column Ordinals
Problem
You want to use column ordinals rather than column names to retrieve data from a DataReader to improve application performance and without hard-coding the ordinal values
Solution
Enumerate the column ordinals using the GetOrdinal( ) method and use those values to retrieve data from the DataReader
The sample code contains two event handlers:
Form.Load
Sets up the sample by using a DataReader to retrieve the ordinals for all columns
in the Orders table of the Northwind database
Go Button.Click
Builds a new DataReader containing the TOP 5 records from the Orders table in the Northwind database The code iterates over the records in the DataReader and demonstrates techniques to retrieve data from the OrderID, CustomerID,
OrderDate, and ShipRegion using accessors that are index-based, nonspecific, provider-specific, nonspecific with null check, and provider-specific with null check
The C# code is shown in Example 9-11
Example 9-11 File: DataReaderColumnOrdinalForm.cs
// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Text;
using System.Data;
using System.Data.SqlClient;
private int co_OrderId;
private int co_CustomerId;
Trang 2private int co_EmployeeId;
private int co_OrderDate;
private int co_RequiredDate;
private int co_ShippedDate;
private int co_ShipVia;
private int co_Freight;
private int co_ShipName;
private int co_ShipAddress;
private int co_ShipCity;
private int co_ShipRegion;
private int co_ShipPostalCode;
private int co_ShipCountry;
//
private void OptimizingDataRetrievalDataReaderForm_Load(object sender, System.EventArgs e)
{
String sqlText = "SELECT * FROM Orders";
// Create the connection and command
SqlConnection conn = new SqlConnection(
ConfigurationSettings.AppSettings["Sql_ConnectString"]);
SqlCommand cmd = new SqlCommand(sqlText, conn);
conn.Open( );
// Create the DataReader to retrieve ordinals
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SchemaOnly);
// Retrieve the column ordinals for each field
co_OrderId = dr.GetOrdinal("OrderID");
co_CustomerId = dr.GetOrdinal("CustomerID");
co_EmployeeId = dr.GetOrdinal("EmployeeID");
co_OrderDate = dr.GetOrdinal("OrderDate");
co_RequiredDate = dr.GetOrdinal("RequiredDate");
co_ShippedDate = dr.GetOrdinal("ShippedDate");
co_ShipVia = dr.GetOrdinal("ShipVia");
co_Freight = dr.GetOrdinal("Freight");
co_ShipName = dr.GetOrdinal("ShipName");
co_ShipAddress = dr.GetOrdinal("ShipAddress");
co_ShipCity = dr.GetOrdinal("ShipCity");
co_ShipRegion = dr.GetOrdinal("ShipRegion");
co_ShipPostalCode = dr.GetOrdinal("ShipPostalCode");
Trang 3co_ShipCountry = dr.GetOrdinal("ShipCountry");
// Close the DataReader and connection
dr.Close( );
conn.Close( );
}
private void goButton_Click(object sender, System.EventArgs e) {
StringBuilder sb = new StringBuilder( );
String sqlText = "SELECT TOP 5 * FROM Orders";
// Create the connection and command
SqlConnection conn = new SqlConnection(
ConfigurationSettings.AppSettings["Sql_ConnectString"]); SqlCommand cmd = new SqlCommand(sqlText, conn);
conn.Open( );
// Create the DataReader to retrieve data
SqlDataReader dr = cmd.ExecuteReader( );
// Iterate over the records in the DataReader
while(dr.Read( ))
{
// Access the fields based on their column ordinals
sb.Append(
// Index-based access
"OrderID: " + dr[co_OrderId] + Environment.NewLine + // Nonspecific accessor
"CustomerID: " + dr.GetString(co_CustomerId) +
Environment.NewLine +
// Provider-specific accessor
"OrderDate: " + dr.GetSqlDateTime(co_OrderDate) + Environment.NewLine +
// Nonspecific accessor with NULL
"ShipRegion (non-specific): " +
(dr.IsDBNull(co_ShipRegion) ? "null" :
dr.GetString(co_ShipRegion)).ToString( ) +
Environment.NewLine +
// Provider-specific accessor with NULL
"ShipRegion (Sql-specific): " +
(dr.GetSqlString(co_ShipRegion).IsNull ? "null" :
Trang 4dr.GetSqlString(co_ShipRegion)).ToString( ) +
Environment.NewLine + Environment.NewLine);
}
// Close the DataReader and connection
dr.Close( );
conn.Close( );
resultTextBox.Text = sb.ToString( );
}
Discussion
The GetOrdinal( ) method of the DataReader object gets the column ordinal for a
specified column name As discussed in Recipe 9.6, reading data from a DataReader is significantly faster using column ordinals instead of column names The GetOrdinal( ) method can be used in the constructor to retrieve all column ordinals based on the column names Column ordinals can then be used to read data from the DataReader to improve performance without having to code absolute column ordinal values
The GetName( ) method of the DataReader takes a column ordinal and returns the
column name
[ Team LiB ]