using System;
using System.Data;
using System.Data.SqlClient;
class OrdinalIndexer
{
static void Main(string[] args)
{
string connString = @"server = .\sqlexpress;integrated security = true;database = northwind";
string sql = @"select companyname,contactname from customers where contactname like 'M%'";
SqlConnection conn = new SqlConnection(connString);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(" {0} | {1}",
rdr[0].ToString().PadLeft(25),
rdr[1].ToString().PadLeft(20));
}
rdr.Close();
}
catch (Exception e)
{
Console.WriteLine("Error Occurred: " + e);
}
finally
{
conn.Close();
}
}
}
|