Execute Scalar : IDbCommand « ADO.Net « C# / CSharp Tutorial






using System;
using System.Data;
using System.Data.SqlClient;

class MainClass
{
    public static void Main()
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
            con.Open();

            // Create and configure a new command.
            IDbCommand com = con.CreateCommand();
            com.CommandType = CommandType.Text;
            com.CommandText = "SELECT COUNT(*) FROM Employee";
    
            // Execute the command and cast the result.
            int result = (int)com.ExecuteScalar();
    
            Console.WriteLine("Employee count = " + result);
        }
    }
}








32.43.IDbCommand
32.43.1.Update data using SQL update clause
32.43.2.Execute Scalar