This article describes how to use the Getxxx
methods (such as GetChar
, and GetInt32
) to avoid the boxing penalty when you use the DataReader
Description of the Technique
When you use the Item
property to read columns from a DataReader
, the values are boxed and then unboxed. If the values are repeatedly boxed and unboxed, the heap quickly fills and increases the frequency of garbage collections. This also slightly impacts performance because Microsoft Visual Studio .NET converts and copies data more than necessary.NOTE
: Boxing means that the data is copied onto the heap as System.Object
. When you cast to a specific data type, the values are unboxed and copied onto your variables on the stack or into another object.
To avoid the boxing penalty, use the Getxxx
methods (such as GetChar
, and GetInt32
) that return the data as a simple data type instead of as System.Object
: The Item
property is the indexer for the SqlDataReader
class in Visual C# .NET
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
- Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
- Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
- Visual Studio .NET
- ADO.NET fundamentals and syntax
Create Project and Add Code
- Start Visual Studio .NET.
- Create a new Windows Application project in Visual C# .NET. By default, Form1 is added to the project.
- Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.
- Add a Command button to Form1. Change the Name property of the button to btnTest, and change the Text property to Test.
- Use the using statement on the System, System.Data, and System.Data.SqlClient namespaces so that you do not have to qualify declarations in those namespaces later in your code. Add the following code to the General Declarations section of Form1:
- Add the following code in the btnTest_Click event:
String myConnString =
"User ID=myUid;password=myPwd;Initial Catalog=Northwind;Data Source=myServer";
String mySelectQuery =
"Select * From Customers";
SqlConnection con = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery, con);
SqlDataReader myReader = myCommand.ExecuteReader();
String str1 = "";
//This code uses the GetString method.
//str1 = str1 + myReader.GetString(0) + ", ";
//This code uses the Indexer for myReader.
str1 = str1 + myReader + ", ";
- Modify the connection string (myConnString) as appropriate for your environment.
- Save your project. On the Debug menu, click Start to run your project.
- Click Test. Notice that the query uses one of the methods (GetString or Item) to return data, and the message boxes display this data.
- To compare the difference in performance or to time the code, use the QueryPerformanceCounter function to time application code. For more information about QueryPerformanceCounter, see the REFERENCES section.
The disadvantage of using the Getxxx
method is that you must check for NULL before you access the field. To check for NULL, use the IsDBNull
For more information about ADO.NET objects and syntax, see the following topic in the Microsoft .NET Framework Software Development Kit (SDK) documentation or MSDN Online:
For additional information about how to use QueryPerformanceCounter
to time code in Visual Basic .NET, click the article number below to view the article in the Microsoft Knowledge Base:
How To Use QueryPerformanceCounter to Time Code in Visual C# .NET
Article ID: 312855 - Last Review: January 17, 2015 - Revision: 3.5
- Microsoft ADO.NET 1.1
- Microsoft ADO.NET 1.0
- Microsoft Visual C# .NET 2003 Standard Edition
- Microsoft Visual C# .NET 2002 Standard Edition
|kbnosurvey kbarchive kbhowtomaster kbsqlclient kbsystemdata KB312855|