using System;
using System.Data.SqlClient;
using System.Data.SqlTypes;
class Program
{
static void Main(string[] args)
{
string sqlConnectString = "Data Source=(local);Integrated security=SSPI;Initial Catalog=AdventureWorks;";
string sqlSelect = "SELECT TOP 5 ContactID, FirstName, MiddleName, LastName FROM Person.Contact";
SqlConnection connection = new SqlConnection(sqlConnectString);
SqlCommand command = new SqlCommand(sqlSelect, connection);
connection.Open();
using (SqlDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
int contactID = dr.GetInt32(0);
String firstName = dr.GetString(1);
String middleName = dr.IsDBNull(2) ? null : dr.GetString(2);
String lastName = dr.GetString(3);
Console.WriteLine("{0}\t{1}, {2} {3}",contactID, lastName, firstName, middleName);
}
}
}
}
|