Consider the following SQL statement:
string MySQLString = @"Select count(*) from customer
where city = 'Walla Walla'";
Your first reaction might be to use this statement to fill a DataSet
with a one row, one column Table in order to get the count. This would
work, but why have all the overhead of a DataSet? A better approach is
to use the ExecuteScalar() method instead:
OleDbConnection loConnection = new OleDbConnection(MyConnectString);
loConnection.Open();
OleDbCommand cmd = new OleDbCommand(MySQLString, loConnection);
int iCount = Convert.ToInt32(cmd.ExecuteScalar());
Note the use of Convert.ToInt32 ... this is to keep your code back-end
independent, as some databases may return a count other than an integer.
from a solution provided by Çetin Basöz in Message #1005874 and Message #1006094
|