The release number of this article has been CHS312855
For Microsoft Visual Basic .NET versions of this article, see
310348.
This article references the following Microsoft .NET Framework Class Bank Name Space:
System.data.oledb system.data.sqlclient
This task content
summary
Technical Description Requirements Creating Projects and Add Code Troubleshooting Reference
Summary This article is introduced in use
How to use the DataReader object
Get XXX method (such as
GetChar,
GetDouble and
GetInt32) to avoid packing.
Back to top
Technical description is in use
Item property
When the DataReader reads the column, the value is packaged and then removed. If the value is reversed and removed, the pile will quickly fill and increase the frequency of garbage recovery. This will also affect performance because Microsoft Visual Studio .NET will convert and copy data too much.
Remarks: Packing means data
System.Object is replicated onto the pile. When converting to a specific data type, the value is removed and copied to the variable on the stack or copied to another object.
To avoid packing losses, please use Get
XXX method (such as
GetChar,
Getdouble,
GetInt32, etc.), these methods use data in simple data types and
System.Object is returned.
Remarks:
The item attribute is Visual C # .NET
SqlDataReader class indexer
Back to top
Require the following list lists the recommended hardware, software, network structure, and service pack required:
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 Foundation and Syntax
Back to top
Create projects and add code
Start Visual Studio .NET. Create a Windows application project in Visual C # .NET. The Form1 is added to the project by default. Make sure your project contains a reference to System.Data namespace, if not included, add a reference to this namespace. Place a command button on Form1. Change the button's Name property to btnTest to change the Text property to Test. Using the USING statement for System, System.Data, and System.Data.sqlclient namespace, you don't need to limit the declarations in these namespaces in your code. Add the following code to the "General Declarations" section of the Form1: use system;
Using system.data.oledb;
Using system.data.sqlclient; Add the following code to 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);
C.Open ();
SqldataReader myReader = mycommand.executeReader ();
String str1 = "";
While (MyReader.Read ())
{
// this code Uses the getString method.
// str1 = str1 myReader.getstring (0) ",";
// this code Uses the indexer for myReader.
STR1 = STR1 MYReader [0] ",";
}
Messagebox.show (str1);
MyReader.Close ();
Conit (); modify the connection string (MyConnString) based on your environment. Save the project. On the Debug menu, click Start to run your project. Click Test. Note that the query will use a method (getString or Item) to return data, and the message box will display this data. To compare the difference in performance or when the measurement code is running, use the QueryPerFormanceCounter function to measure the run of the application code. For more information on QueryPerFormanceCounter, see Reference section.
Back to top
Troubleshooting using Get
The disadvantage of the XXX method must first check whether NULL before accessing the field. To check if it is null, please use
ISDBNULL method.
Back to top
Refer to more information about ADO.NET objects and syntax, see the Microsoft .NET Framework Software Development Kit (SDK) document or the following topics in MSDN Online:
Access the data using ADO.NET http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaccessingDataWithadonet.asp About how to use in Visual Basic .NET
QueryperFormanceCounter to measure additional information about the running of the code, click the article number below to view the article in the Microsoft Knowledge Base:
306979 How to: Use QueryPerformanceCounter when using QueryPerformanceCounter in Visual C # .NET
Back to top
The information in this article applies to:
Microsoft ADO.NET (provided with .NET Frame) Microsoft Visual C # .NET (2002)
Recent Update: 2002-8-6 (1.0) Keyword KBDsupport KBGRPDSMDAC KBGRPDSVBDB KBHOWTO KBHOWTOMASTER KBOLEDB KBSQLCLIENT KBSYSTEMDATA KB312855