how to know the number of records in a table?

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!
 
M

Marina

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

RA

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top