using System;
using System.Data;
using System.Data.SqlClient;
class CommandReader
{
static void Main()
{
SqlConnection conn = new SqlConnection(@"server = .\sqlexpress;integrated security = true;database = northwind");
string sql = @"select firstname,lastname from employees";
SqlCommand cmd = new SqlCommand(sql, conn);
Console.WriteLine("Command created and connected.");
try
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine("Employee name: {0} {1}",
rdr.GetValue(0),
rdr.GetValue(1)
);
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close();
Console.WriteLine("Connection Closed.");
}
}
}
|