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 = (int)dr.GetSqlInt32(0);
String firstName = (string)dr.GetSqlString(1);
String middleName = dr.IsDBNull(2) ? null :(string)dr.GetSqlString(2);
String lastName = (string)dr.GetSqlString(3);
Console.WriteLine("{0}\t{1}, {2} {3}",contactID, lastName, firstName, middleName);
}
}
}
}
|