tailieunhanh - Improving DataReader Performance with Typed Accessors

[ Team LiB ] Recipe Improving DataReader Performance with Typed Accessors Problem You need to improve performance when accessing data from a DataReader. | Team LiB Recipe Improving DataReader Performance with Typed Accessors Problem You need to improve performance when accessing data from a DataReader. Solution Use DataReader typed accessors to improve performance by eliminating repeated boxing and unboxing of object data to and from .NET Framework data types. The sample code measures the time to access data in a DataReader using three techniques typed accessor column ordinal and column name. The user specifies the technique by selecting a radio button. To ensure accuracy in each case the routine reads all data from the DataReader 100 times and measures the total time in ticks which are 100-nanosecond intervals. The C code is shown in Example 9-10. Example 9-10. File Namespaces variables and constants using System using using using using . . . int orderld String customerld int employeeld DateTime orderDate DateTime requiredDate DateTime shippedDate int shipVia Decimal freight String shipName String shipAddress String shipCity String shipRegion String shipPostalCode String shipCountry String sqlText SELECT OrderlD CustomerlD EmployeelD OrderDate RequiredDate ShippedDate ShipVia Freight ShipName ShipAddress ShipCity ShipRegion ShipPostalCode ShipCountry FROM Orders Create the connection and the command. SqlConnection conn new SqlConnection Sql_ConnectString SqlCommand cmd new SqlCommand sqlText conn String accessMethod Typed accessor Ordinal Column name int startTick 0 int elapsedTick 0 for int i 1 i 100 i Create the DataReader and retrieve all fields for each record in the DataReader according to user request. SqlDataReader dr if startTick while orderld 0 if 1 customerld