Friday, October 14, 2011

Friday 10.14.11

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

namespace RetrieveSingleValueFromQuery
{
class Program
{
//the ExecuteScalar() method of the Command object returns a single value from the dta soruce rather than a collection of records as
//a table or data stream
static void Main(string[] args)
{
string sqlConnectString = @"Data Source=Alfred-PC\SQLExpress; Integrated security=SSPI; Initial Catalog=AdventureWorks;";

string sqlSelect = "SELECT COUNT(*) FROM Person.Contact";

SqlConnection connection = new SqlConnection(sqlConnectString);

//create the scalar command and open the connection
SqlCommand command = new SqlCommand(sqlSelect, connection);
connection.Open();

//execute the scalar SQl statement and store resuts.
int count = Convert.ToInt32(command.ExecuteScalar());
connection.Close();

Console.WriteLine("Record count in Person.Contact = {0}", count);

Console.WriteLine();
Console.ReadLine();
}
}
}

No comments:

Post a Comment