Kurusu
Kurusu

Reputation: 197

How do I retrieve the number of rows affected from SQL Server to VB.NET?

Basically I'm retrieving all the data in my program through runtime, I was wondering how will I retrieve the number of rows affected after an update so I could prompt the user about it through VB.NET

What I'm actually doing is, after an update, if there are no other rows updated then the user can no longer click on the button

Upvotes: 6

Views: 22231

Answers (4)

By using ExecuteNonQuery, it will returns no rows, any output parameters or return values mapped to parameters are populated with data.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

EDIT:

You can prompt the User like

Dim RowsAffected as Integer = Command.ExecuteNonQuery()
MsgBox("The no.of rows effected by update query are " & RowsAffected.ToString)

Upvotes: 12

Purplegoldfish
Purplegoldfish

Reputation: 5284

You can update your statements to return the rowcount value.

This should be helpful http://technet.microsoft.com/en-us/library/ms187316.aspx

Upvotes: 0

Marco
Marco

Reputation: 57583

You can use SqlCommand for this:

Dim cmd As SqlCommand
Dim rows_Affected as Integer
rows_Affected = cmd.ExecuteNonQuery()

Upvotes: 2

Simon
Simon

Reputation: 6152

If you're using the SQLCommand object directly then a call to ExecuteNonQuery will return a count of rows affected:

Dim I as Integer= MyCommandObject.ExecuteNonQuery()

Hope this makes sense.

Upvotes: 4

Related Questions