using System;
using System.Data;
using System.Data.SqlClient;
class ResultSetInfo
{
static void Main(string[] args)
{
string connString = @"server = .\sqlexpress;integrated security = true;database = northwind";
string sql = @"select contactname,contacttitle from customers where contactname like 'M%'";
SqlConnection conn = new SqlConnection(connString);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rdr = cmd.ExecuteReader();
Console.WriteLine("Column Name:\t{0} {1}",rdr.GetName(0),rdr.GetName(1));
Console.WriteLine("Data Type:\t{0} {1}",rdr.GetDataTypeName(0),rdr.GetDataTypeName(1));
while (rdr.Read())
{
Console.WriteLine("{0} {1}",rdr.GetString(0),rdr.GetString(1));
}
Console.WriteLine("Number of columns in a row: {0}",rdr.FieldCount);
Console.WriteLine(rdr.GetName(0));
Console.WriteLine(rdr.GetOrdinal("contactname"));
Console.WriteLine(rdr.GetFieldType(0));
Console.WriteLine(rdr.GetName(1));
Console.WriteLine(rdr.GetOrdinal("contacttitle"));
Console.WriteLine(rdr.GetFieldType(1));
rdr.Close();
}
catch (Exception e)
{
Console.WriteLine("Error Occurred: " + e);
}
finally
{
conn.Close();
}
}
}