// From http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlCommandClassTopic.asp // Note: The connection string is passed in as a parameter. // You don't have to do that. You can declare it in // the body of the method if you like. public void ReadMyData(string myConnString) { string mySelectQuery = "SELECT OrderID, Customer FROM Orders"; SqlConnection myConnection = new SqlConnection(myConnString); SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection); myConnection.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); try { while (myReader.Read()) { Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1)); } } finally { // always call Close when done reading. myReader.Close(); // always call Close when done reading. myConnection.Close(); } }