Using System. Data; // Use ADO. NET namespace
Using System. Data. SqlClient;
SqlConnection thisConnection = new SqlConnection (
@ "Data Source = GY; Initial Catalog = northwind; uid = sa; password = datadog"); // first establish a connection
ThisConnection. Open (); // Open the connection
SqlCommand thisCommand = thisConnection. CreateCommand (); // directly specify the conn command
Alternatively, // SqlCommand cmd = new SqlCommand ();
// Cmd. Connection = thisConnection;
SQL command of cmmand
ThisCommand. CommandText = "SELECT CustomerID, CompanyName from Customer ";
SqlDataReader thisReader = thisCommand. ExecuteReader (); // sqldatareader cannot be used. new must be directly associated with command
// Read the table with reader
While (thisReader. Read ())
{
// Output ID and name columns
Console. WriteLine ("{0} {1 }",
ThisReader ["CustomerID"], thisReader ["CompanyName"]);
// Close reader to Close when used up
ThisReader. Close ();
// Close connection
ThisConnection. Close ()