I wrote my own SQL client because I was tired of SSMS. I want to capture the affected row counts just like SSMS but SqlCommand.StatementCompleted
is raised multiple times when ExecuteReader
finishes (or EndExecuteReader
is called). Even though the statements in the batch complete with a regular interval, it seems like the DONE_IN_PROC messages are queued up on the client, and then dumped all at once, instead of being raised continuously. No InfoMessage
events through-out the execution from SqlConnection
, either.
Update above in italics.
Say, you have a SQL statement that updates rows in a loop, or something like that:
WHILE 1=1
BEGIN
UPDATE tbl SET .... WHERE Id BETWEEN i AND i+10;
IF @@ROWCOUNT =0 BREAK;
SET i = i + 10;
END
SSMS properly shows "(10 rows affected)" every X number of seconds or w/e.
There are no records coming from the reader, since this is a UPDATE only statement, so can't use SqlDataReader
to count the rows.
Is that possible, using the SqlClient
library?
The simplified code in question is like this:
class ExecuteWorker
{
public void Start(string query)
{
this._query = query;
this._thread.Start(this.Work);
}
void Work()
{
using(var conn = new SqlConnection(this._connStr))
{
conn.Open();
using(var command = conn.CreateCommand())
{
command.CommandText = this._query;
using(var reader = command.ExecuteReader())
{
while(reader.Read())
{
this._control.BeginInvoke(new Action(()=>{
// update UI
}));
}
}
}
}
}
}
A similar question is found here, though the description is less clear.