Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=northwind");
string sql = @"select contactname from customers";
try
{
conn.Open();
//create the command
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("{0}",reader[0]);
} reader.Close();
}
catch (SqlException ex)
{
Console.WriteLine("The error:" + ex.Message);
}
finally
{
conn.Close();
Console.WriteLine("The end!");
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=northwind");
string sql = @"select contactname from customers";
try
{
conn.Open();
//create the command
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("{0}",reader[0]);
} reader.Close();
}
catch (SqlException ex)
{
Console.WriteLine("The error:" + ex.Message);
}
finally
{
conn.Close();
Console.WriteLine("The end!");
}
Console.ReadLine();
}
}
}