Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string connString = @"Data Source=S013;Initial Catalog=Northwind;Integrated Security=True";
string sql = @"select * from employees";
SqlConnection conn = null;
SqlDataReader reader=null;
try
{
conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
reader = cmd.ExecuteReader();
Console.WriteLine("This program demonstrrates the use of the SQL Server Data Provider.");
Console.WriteLine("Querying database {0} with query {1}\n",conn.Database,cmd.CommandText);
Console.WriteLine("First name\tLastName\n");
while (reader.Read())
{
Console.WriteLine("{0}|{1}",reader["firstname"].ToString().PadLeft(10),reader[1].ToString().PadLeft(10));
}
}
catch (SqlException ex)
{
Console.WriteLine("Error:"+ex);
}
finally
{
reader.Close();
conn.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string connString = @"Data Source=S013;Initial Catalog=Northwind;Integrated Security=True";
string sql = @"select * from employees";
SqlConnection conn = null;
SqlDataReader reader=null;
try
{
conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
reader = cmd.ExecuteReader();
Console.WriteLine("This program demonstrrates the use of the SQL Server Data Provider.");
Console.WriteLine("Querying database {0} with query {1}\n",conn.Database,cmd.CommandText);
Console.WriteLine("First name\tLastName\n");
while (reader.Read())
{
Console.WriteLine("{0}|{1}",reader["firstname"].ToString().PadLeft(10),reader[1].ToString().PadLeft(10));
}
}
catch (SqlException ex)
{
Console.WriteLine("Error:"+ex);
}
finally
{
reader.Close();
conn.Close();
}
}
}
}