public void second()
{
string strsql = "data source=KNUZ7ZSDSSK5SAJ;initial catalog=myschool;user id=sa;pwd=123";
SqlConnection con = new SqlConnection(strsql);
try
{
con.Open();
StringBuilder sb = new StringBuilder();
sb.AppendLine("select");
sb.AppendLine(" [studentname]");
sb.AppendLine(" [studentno]");
sb.AppendLine("from");
sb.AppendLine(" [student]");
SqlCommand com = new SqlCommand(sb.ToString(), con);
SqlDataReader reader= com.ExecuteReader();
if(reader==null){
Console.WriteLine("出现异常");
}
Console.WriteLine("学号\t姓名");
reader.Read();
while(reader.Read())
{
sb.AppendFormat("{0}\t{1}", reader["studentname"], reader["studentno"]);
Console.WriteLine(sb);
sb.Length = 0;
}
reader.Close();
}
catch (Exception)
{
Console.WriteLine("null");
}
finally
{
con.Close();
}
}