C#:如何实现:在textbox中显示SQLsever数据库中查询(例如;select grade from student where sno=121101)所得的一个数据?
求大神帮忙啊!!!
using(SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=yourdb"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select grade from student where sno = 121101",conn);
object obj = cmd.ExecuteScalar();
if(obj!=null)
{
this.textbox1.Text = obj.ToString();
}
}
急求啊,在线等啊!!!!!!!!
看一下数据库查询
http://www.cnblogs.com/Martin_Q/archive/2011/03/30/1999950.html
赋值直接textbox.text=字段值
最简单的是
SqlCommand cmd = new SqlCommand(sql, conn);
textBox1.Text = cmd.ExecuteScalar().ToString();
Dim strSQL1 = "select id from userlist where password=1234"
Cmd.CommandText = strSQL1
dim MyReader As SqlDataReader = Cmd.ExecuteReader
If MyReader.HasRows Then
While MyReader.Read
TextBox1.Text=MyReader("ID")
End While
End If
MyReader.Close()
晚上11:40提出的问题,12:10就解决了,这么多人回复,留个记号,太牛了
感觉这个问题就是把sql的查询结果赋给一个变量,然后把变量赋值给textbox,
caozhy说的挺对
SqlCommand cmd = new SqlCommand(sql, conn);
textBox1.Text = cmd.ExecuteScalar().ToString();