开发者

How to get the Affected Records in VB.net 2010 with SQL?

开发者 https://www.devze.com 2023-04-02 05:07 出处:网络
I\'m trying to get the Affected Records but it always give me -1 Here is my code: cmd.CommandText = \"SELECT * FROM persons\"

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消