. , . :
Select count(*) from customers where state = 'NY'
:
bool res = (Convert.ToInt32(cmd.ExecuteScalar()) > 0 ? true : false);
If the query results are greater than 0, set res to true, otherwise res is false. The above code example is redundant and can be rewritten as:
bool res = Convert.ToInt32(cmd.ExecuteScalar()) > 0;
I sometimes use code like this to check if any rows were affected when running a request to insert, update, or delete. Something like that:
public bool MyInsertMethod(){
return cmd.ExecuteNonQuery() > 0;
}
source
share