以sqlserver为例,大概代码如下。
strSql="select studentage from student where studentname="+textBox1.text.trim(); //拼接sql语句(注:这不是最好的方式,会存在注入漏洞,但按你现问的这个问题,还没到要考虑这个漏洞的时候,只要会这么用就可以了)
SqlConnection connection = new SqlConnection("connectionString数据库连接串")
connection.open()
SqlCommand cmd = new SqlCommand(strSql,connection )//connection 为你创建的连接对象
using(SqlDataReader dr = cmd.ExecuteReader())
{
if(dr.Read())
{
textBox2=dr["studentage"].ToString();
}
}
还不明白可参考msdn帮助手册:
http://msdn.microsoft.com/zh-cn/library/9kcbe65k(v=VS.80).aspx