how to know the number of records in a table?

  • Thread starter Thread starter Matthew Louden
  • Start date Start date
M

Matthew Louden

I want to count the number of records in a table, and insert a new record
with ID increment the count by 1.

Heres the code:
sqlStmt = "SELECT COUNT(*) As numRecords FROM TimeSlot;"
cmd = New SqlCommand(sqlStmt, conn)
dr = cmd.ExecuteReader

But how to get the value of numRecords? since numRecords is not a field in
table TimeSlot

please help!
 
You can't until u've read through all of them. The datareader is a forward
only read-only quick way to look at data.

If you need to know the number up front, use a dataset or datatable.
 
This won't work for multiple database connections. The incremeted count
might give you false information. I assume that the ID is unique value? If
yes then change it in the database to be an identity field. If you do that
let me know and I will help you on how to get the new row inserted.

Ronen
 
Back
Top