Csharp/C Sharp/Database ADO.net/Row Count

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

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.rumandText = "SELECT COUNT(*) FROM Products";
        mySqlConnection.Open();
        int returnValue = (int)mySqlCommand.ExecuteScalar();
        Console.WriteLine("mySqlCommand.ExecuteScalar() = " + returnValue);
        mySqlConnection.Close();
    }
}