ExecuteScalar
object result = Command.ExecuteScalar();
Executes a SQL command and returns the first value of the first row from the result set. One common use of this method is to return the results of an aggregate SQL function.
Following is an example that uses the ExecuteScalar( )
method with the SQL aggregate function COUNT to determine
how many rows match a specific criteria.
string SQL = "SELECT COUNT(*) FROM Orders WHERE " + "OrderDate >= '1996-01-01' AND OrderDate < '1997-01-01'"; SqlCommand cmd = new SqlCommand(SQL, con); con.Open(); int result = (int)cmd.ExecuteScalar(); con.Close(); // Display the result of the operation. Console.WriteLine(result.ToString() + " rows in 1996");