Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("server=zhuobin;uid=sa;pwd=zhuobin;database=northwind");
string sql = @"select companyname,contactname from customers where contactname like 'M%'";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader reader = cmd.ExecuteReader();
Console.WriteLine("\t{0}{1}","CompanyName".PadRight(25),"ContactName".PadRight(20));
Console.WriteLine("\t{0}{1}","===========".PadRight(25),"===========".PadRight(20));
while (reader.Read())
{
Console.WriteLine("\t{0}|{1}",reader[0].ToString().PadRight(25),reader[1].ToString().PadRight(20));
}
}
catch (SqlException ex)
{
Console.WriteLine("Error:{0}", ex.Message);
}
finally
{
} Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("server=zhuobin;uid=sa;pwd=zhuobin;database=northwind");
string sql = @"select companyname,contactname from customers where contactname like 'M%'";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader reader = cmd.ExecuteReader();
Console.WriteLine("\t{0}{1}","CompanyName".PadRight(25),"ContactName".PadRight(20));
Console.WriteLine("\t{0}{1}","===========".PadRight(25),"===========".PadRight(20));
while (reader.Read())
{
Console.WriteLine("\t{0}|{1}",reader[0].ToString().PadRight(25),reader[1].ToString().PadRight(20));
}
}
catch (SqlException ex)
{
Console.WriteLine("Error:{0}", ex.Message);
}
finally
{
} Console.ReadLine();
}
}
}