The release number of this article has been CHS308352
For Microsoft Visual Basic .NET versions of this article, see
308050.
This article references the following Microsoft .NET Framework Class Bank Name Space:
System.data system.data.sqlclient
Symptom
OLEDBDATAREADER or
When the SqlDataReader class, there is no
The RecordCount property indicates the number of records being acquired.
the reason
DataReader objects or backend data sources typically do not know the number of recorded records until the last record is sent to the client.
When the ActiveX Data Object (ADO) recordset uses only the forward cursor to retrieve data, it even
The RecordCount property returns -1.
DataReader performs similar behavior because it uses only a forward check-in and columns.
Solution To resolve this issue, use one of the following methods:
The records are counted when the reader is passed. Run the select count (*) query first. Note that this query may be out of time when you read the data.
This phenomenon is designed to make.
More information
Steps to reproduce the phenomenon
Start Microsoft Visual Studio .NET. Create a Windows application in Visual C # .NET. FORM1 will be created 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 and change its Name property to btntest. Use the IMPORTS statement for System and System.Data namespace, so that there is no need to define a declaration in these namespaces in the following code. Using system;
Using system.data;
Using system.data.sqlclient; Return to Forms view, then double-click BTNTest to add a click event handler. Add the following code to the handler: String myConnString =
"User ID = sa; password = sa; initial catalog = pubs; data source = mysqlserver";
String myselectQuery = "select * from authors";
SqlConnection MyConnection = New SqlConnection (MyConnString);
Sqlcommand mycommand = new sqlcommand (MySelectQuery, MyConnection);
MyConnection.open ();
SqlDataReader MyReader;
MyReader = myCommand.executeReader ();
INT recordcount = 0;
Try
{
While (MyReader.Read ())
{
Recordcount ;
}
IF (RecordCount == 0)
MessageBox.show ("no data returned");
Else
Messagebox.show ("Number of Records Returned:" RecordCount);
}
Catch (Exception EX)
{
Messagebox.show (ex.totring ());
}
Finally
{
MyReader.Close ();
MyConnection.Close ();} Modify the connection string (MyConnString) according to your environment. Save the project. On the Debug menu, click Start to run your project. Click this button. Please note that you can see the count of records.
Refer to other information, click the following article number to view the appropriate Microsoft Knowledge Base article:
194973 PRB: ADO: RECOUNT May Return -1 (PRB: ADO: Recordcount may return -1)
The information in this article applies to:
Microsoft ADO.NET (provided with .NET Frame) Microsoft Visual C # .NET (2002)
Recent Updated: 2002-6-17 (1.0) Keyword Kbdsupport KBGRPDSMDAC KBPRB KBSQLClient KbsystemData KB308352