The problem is that my SqlDataReader property .RecordsAffected always returns -1 regardless of whatever kind of select, update, or delete query I run.
Here is my relevant code:
SqlDataReader myReader = null;
SqlCommand command = new SqlCommand("CSP_CT_233", myConnection);
myReader = command.ExecuteReader();
while (myReader.Read())
{
blah blah blah //this stuff works fine
}
myReader.Close();
Console.Writeline(myReader.RecordsAffected.ToString());
SqlCommand command = new SqlCommand("CSP_CT_233", myConnection);
myReader = command.ExecuteReader();
while (myReader.Read())
{
blah blah blah //this stuff works fine
}
myReader.Close();
Console.Writeline(myReader.RecordsAffected.ToString());
8 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
rocketPosted Dec 5, 2008, 5:33 PM
The problem seems to be when my my update where field differs from my set field
(this is updating one record)
update table set field1=1 where field1=0 //returns 1
update table set field1=1 where field2=0 //returns 2
anyone seen this before?
rocketPosted Dec 5, 2008, 10:48 AM
ok now it will return 2 records affected whenever I update 1 row.
Otherwise it will report accurately...
rocketPosted Dec 4, 2008, 5:03 PM
Ryan AlfordPosted Dec 4, 2008, 4:33 PM
rocketPosted Dec 4, 2008, 2:49 PM
ok i am using this now
int affectedRowsInt = command.ExecuteNonQuery();
Console.WriteLine("Affected Rows" + affectedRowsInt .ToString());
and it is still returning -1, I know that it will return -1 when no records are affected but I am double checking that records are actually being affected with SQL server queries, so I know that this is not correct.
Ryan AlfordPosted Dec 4, 2008, 1:31 PM
rocketPosted Dec 4, 2008, 12:27 PM
The problem is that this will not work for UPDATE and DELETE statements only for SELECT statements.
Ryan AlfordPosted Dec 4, 2008, 11:37 AM
or if you are loading the information from the datareader into some type of collection, you could use the count or length of the collection.