Microsoft Knowledge Base Email Alertz

KBAlertz.com: This article describes how to use the

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 310348 - Last Review: May 13, 2007 - Revision: 3.2

How To Avoid the Boxing Penalty When You Use the DataReader in Visual Basic .NET

This article was previously published under Q310348

On This Page

SUMMARY

This article describes how to use the Getxxx methods (such as GetChar, GetDouble, and GetInt32) to avoid the boxing penalty when you use the DataReader object.

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, GetDouble, and GetInt32) that return the data as a simple data type instead of as System.Object.

Requirements

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

  1. Start Visual Studio .NET.
  2. Create a new Windows Application in Visual Basic .NET. By default, Form1 is added to the project.
  3. Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.
  4. Add a Button control to Form1. Change the Name property of the button to btnTest, and change the Text property to Test.
  5. Use the Imports statement on the System and System.Data namespaces so that you do not have to qualify declarations in those namespaces later in your code. Add this code to the "General Declarations" section of Form1:
    Imports System
    Imports System.Data
    Imports System.Data.OleDb
    Imports System.Data.SqlClient
    					
  6. Copy and paste the following code in the btnTest_Click event:
        Dim myConnString as String = _
            "User ID=sa;password=sa;Initial Catalog=northwind;Data Source=myServer"
        Dim mySelectQuery As String = _
            "Select * From Customers"
        Dim con As New SqlConnection(myConnString)
        Dim myCommand As New SqlCommand(mySelectQuery, con)
            
        con.Open()
        Dim myReader As SqlDataReader = myCommand.ExecuteReader()
        Dim str1 As String
    
        While myReader.Read()
            'This code uses the GetString method.
            'str1 = str1 & myReader.GetString(0) & ", "
            'This code uses the Item method.
            str1 = str1 & myReader.Item(0) & ", "
        End While
        
        MessageBox.Show(str1)
    
        myReader.Close()
        con.Close()
    					
  7. Modify the connection string (myConnString) as appropriate for your environment.
  8. Save your project. On the Debug menu, click Start to run your project.
  9. Click Test. Notice that the query uses one of the methods (GetString or Item) to return data, and the message boxes display this data.
  10. 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.

Pitfalls

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 method.

REFERENCES

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:
Accessing Data with ADO.NET
http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx)
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:
306978  (http://kbalertz.com/Feedback.aspx?kbNumber=306978/EN-US/ ) How To Use QueryPerformanceCounter to Time Code in Visual Basic .NET

APPLIES TO
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET 1.1
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
Keywords: 
kbhowtomaster kbsqlclient kbsystemdata KB310348
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please