string connectionStr = "Server = DESKTOP-IDK01B1\\SQLEXPRESS2014; DataBase = StudentManageDB; Uid = sa; Pwd = sa";
SqlConnection conn = new SqlConnection(connectionStr);
conn.Open();
string sql = "select * from Students";
SqlCommand command = new SqlCommand(sql, conn);
//执行查询方法,返回DataReader对象
SqlDataReader objReader = command.ExecuteReader();
//读取数据
while(objReader.Read())//用Read()方法判断是否有数据
{
Console.WriteLine(objReader["StudentId"].ToString() + "\t" + objReader["StudentName"] + "\t" + objReader["Gender"]);
}
//关闭读取器
objReader.Close();
conn.Close();//先关闭读取器,再关闭连接,顺序不可调换