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());

Ryan AlfordRank #1,411 · 1.2K reputationPosted Dec 4, 2008, 6:37 AM EST
SqlDataReader myReader = null;SqlCommand command = new SqlCommand("CSP_CT_233", myConnection);myReader = command.ExecuteReader();int counter = 0;while (myReader.Read()){ counter++; blah blah blah //this stuff works fine}myReader.Close();Console.Writeline(counter);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.
rocket25 reputationPosted Dec 4, 2008, 7:27 AM EST
The problem is that this will not work for UPDATE and DELETE statements only for SELECT statements.
Ryan AlfordRank #1,411 · 1.2K reputationPosted Dec 4, 2008, 8:31 AM EST
rocket25 reputationPosted Dec 4, 2008, 9:49 AM EST
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 AlfordRank #1,411 · 1.2K reputationPosted Dec 4, 2008, 11:33 AM EST
rocket25 reputationPosted Dec 4, 2008, 12:03 PM EST
rocket25 reputationPosted Dec 5, 2008, 5:48 AM EST
ok now it will return 2 records affected whenever I update 1 row.
Otherwise it will report accurately...
rocket25 reputationPosted Dec 5, 2008, 12:33 PM EST
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?