- Count the records as you go through the reader.
- Run a SELECT COUNT(*) query first (although this may be out of date by the time you finish reading data).
Steps to Reproduce the Behavior
- Open Visual Studio .NET.
- Create a new Windows Application in Visual Basic .NET.
- Make sure that your project contains a reference to the System.Data namespace.
- Place a Command button on Form1 and change its Name property to btnTest.
- Use the Imports statement on the System and System.Data namespaces so that you are not required to qualify declarations in those namespaces later in your code.
- Paste the following code in the code window after the region "Windows Form Designer generated code":
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Dim myConnString As String = _
"User ID=sa;password=sa;Initial Catalog=pubs;Data Source=mySQLServer"
Dim mySelectQuery As String = "SELECT * FROM Authors"
Dim myConnection As New SqlConnection(myConnString)
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
Dim myReader As SqlDataReader
myReader = myCommand.ExecuteReader()
Dim RecordCount As Integer
RecordCount = RecordCount + 1
If RecordCount = 0 then
MessageBox.Show("No data returned")
MessageBox.Show("Number of Records returned: " & RecordCount)
Catch ex As Exception
- Modify the Connection string (myConnString) as appropriate for your environment.
- Save your project. On the Debug menu, click Start, and run your project.
- Click the button and you can see the record count.
ID do Artigo: 308050 - Última Revisão: 23 de mar de 2009 - Revisão: 1