asp.net查询单条记录,

来源:百度知道 编辑:UC知道 时间:2024/05/03 10:50:06
asp.net查询单条记录,
并取出这条记录里的值
例 :数据库:student,表:chengji
表中有两个字段id和name
我现在需要取出id为5的学生的姓名,并保存到变量xingming中

string strConnection="user id=sa;password=;";
strConnection+="initial catalog=student;Server=(local);";
strConnection+="Connect Timeout=30"; //构造连接字符串

SqlConnection conn=new SqlConnection(strConnection); //连接对象

string sql="select name from chengji where id='5'";
SqlCommand myconn = new SqlCommand(sql,conn);
conn.Open();
SqlDataReader read = myconn.ExecuteReader();
read.Read();
string xingming = read["name"].ToString();
read.Close();
conn.Close();

用SQL语句
select name from chengji where id=5