use ExecuteScalar() to run a SELECT statement that returns a single value
using System;
using System.Data;
using System.Data.SqlClient;
class ExecuteScalar {
public static void Main() {
SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = "SELECT COUNT(*) FROM Products";
mySqlConnection.Open();
int returnValue = (int)mySqlCommand.ExecuteScalar();
Console.WriteLine("mySqlCommand.ExecuteScalar() = " + returnValue);
mySqlConnection.Close();
}
}
Related examples in the same category