วันพุธที่ 4 กรกฎาคม พ.ศ. 2550

connect to a sql database using C#

using System;
using System.Data.SqlClient;

namespace ConsoleCSharp
{
///
/// Summary description for Class1.
///

class DataReader_SQL
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
try
{
SqlConnection thisConnection = new SqlConnection(@"Network Library=DBMSSOCN;Data Source=192.168.0.100,1433;database=Northwind;User id=Paladine;Password=;");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers";
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}", thisReader["CustomerID"], thisReader["CompanyName"]);
}
thisReader.Close();
thisConnection.Close();

}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}

}
}
}