How to get the Affected Records in VB.net 2010 with SQL?
I'm trying to get the Affected Records but it always give me -1
Here is my code:
cmd.CommandText = "SELECT * FROM persons"
Dim lrd As SqlData开发者_高级运维Reader = cmd.ExecuteReader()
MessageBox.Show(lrd.RecordsAffected)
What is the problem?
Thanks.
There is nothing affected in the database when you perform a SELECT statement. If you want to count the number of rows use SELECT count(*) FROM persons
and then use ExecuteScalar()
instead of a ExecuteReader()
. And finally if you are really affecting something in the database with either INSERT, UPDATE or DELETE use ExecuteNonQuery()
which will return you the number of affected rows in the database.
精彩评论