using System;
using System.Data;
using System.Data.SqlClient;
class TypedAccessors
{
static void Main(string[] args)
{
string connString = @"server = .\sqlexpress;integrated security = true;database = northwind";
string sql = @"select productname, unitprice, unitsinstock,discontinued from products";
SqlConnection conn = new SqlConnection(connString);
try{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr.GetString(0));
Console.WriteLine(rdr.GetDecimal(1));
Console.WriteLine(rdr.GetInt16(2));
Console.WriteLine(rdr.GetBoolean(3));
}
rdr.Close();
}
catch (Exception e)
{
Console.WriteLine("Error Occurred: " + e);
}
finally
{
conn.Close();
}
}
}
|