using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace DataReaderExample { class Program { static void Main(string[] args) { try { SqlConnection conn = new SqlConnection(); conn.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=AdventureWorks;User Id=sa;Password=v044728V;"; conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT ContactId, FirstName, LastName FROM Person.Contact WHERE FirstName = 'Kim'"; SqlDataReader r = cmd.ExecuteReader(); while (r.Read()) { Console.WriteLine("{0}, {1} {2}", r.GetInt32(0), r.GetString(1), r.GetString(2)); } conn.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } } }