Learning ADO.NET 3.5 Cookbook:(4) Improving DataReader Performance with Typed Accessors

Problem

You need to improve performance when accessing data from a DataReader.

Output for DataReaderTypedAccessors solution


ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Data;
using System.Data.SqlClient;

namespace DataReaderTypedAccessors
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
int loops = 1000;

            
int contactID;
            
string firstName;
            
string middleName = null;
            
string lastName;

            
int startTick = 0;
            
int elapsedTick;

            
string sqlConnectString = "Data Source=(local);" +
                
"Integrated security=SSPI;Initial Catalog=AdventureWorks;";

            
string sqlSelect = "SELECT ContactID, FirstName, MiddleName, LastName " +
                
"FROM Person.Contact";

            Console.WriteLine(
"---DataReader column value access timing test, " +
                
"{0} iterations---\n", loops);

            SqlConnection connection 
= new SqlConnection(sqlConnectString);

            
// Create the command and open the connection
            SqlCommand command = new SqlCommand(sqlSelect, connection);
            connection.Open();

            elapsedTick 
= 0;
            
for (int i = 0; i < loops; i++)
            {
                
// Create the DataReader and retrieve all fields for each
                
// record using a typed accessor with a column ordinal
                using (SqlDataReader dr = command.ExecuteReader())
                {
                    startTick 
= Environment.TickCount;
                    
while (dr.Read())
                    {
                        contactID 
= dr.GetInt32(0);
                        firstName 
= dr.GetString(1);
                        middleName 
= dr.IsDBNull(2? null : dr.GetString(2);
                        lastName 
= dr.GetString(3);
                    }
                    elapsedTick 
+= Environment.TickCount - startTick;
                }
            }
            Console.WriteLine(
"Typed accessor: Ticks = {0}",
                elapsedTick);

            elapsedTick 
= 0;
            
for (int i = 0; i < loops; i++)
            {
                
// Create the DataReader and retrieve all fields for each
                
// record using a column ordinal
                using (SqlDataReader dr = command.ExecuteReader())
                {
                    startTick 
= Environment.TickCount;
                    
while (dr.Read())
                    {
                        contactID 
= Convert.ToInt32(dr[0]);
                        firstName 
= Convert.ToString(dr[1]);
                        middleName 
= Convert.ToString(dr[2]);
                        lastName 
= Convert.ToString(dr[3]);
                    }

                    elapsedTick 
+= Environment.TickCount - startTick;
                }
            }
            Console.WriteLine(
"Column ordinal: Ticks = {0}", elapsedTick);

            elapsedTick 
= 0;
            
for (int i = 0; i < loops; i++)
            {
                
// Create the DataReader and retrieve all fields for each
                
// record using a column field name
                using (SqlDataReader dr = command.ExecuteReader())
                {
                    startTick 
= Environment.TickCount;
                    
while (dr.Read())
                    {
                        contactID 
= Convert.ToInt32(dr["ContactID"]);
                        firstName 
= Convert.ToString(dr["FirstName"]);
                        middleName 
= Convert.ToString(dr["MiddleName"]);
                        lastName 
= Convert.ToString(dr["LastName"]);
                    }

                    elapsedTick 
+= Environment.TickCount - startTick;
                }
            }
            Console.WriteLine(
"Column name: Ticks = {0}", elapsedTick);

            Console.WriteLine(
"\nPress any key to continue.");
            Console.ReadKey();
        }
    }
}

 

转载于:https://www.cnblogs.com/wispzone/archive/2009/08/26/1554518.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值