C# winfrom 如何将SQL中的一条数据显示在 FROM 中的textbox 中?

来源:百度知道 编辑:UC知道 时间:2024/05/10 23:25:10
在C# winfrom中, 如何将SQL中的一条数据显示在 FROM 中的textbox 中?现在问题是:将数据库查询的一条数据,有 id 、name 、age 三个字段,将起显示在 窗体中的 textbox1、textbox2、textbox3中。该如何实现?麻烦给个详细代码吧,谢谢!!!
我要的是在窗体打开时自动将数据加载到 textbox 中,但是在Form_Load(object sender, EventArgs e)中写上去代码实现不了.怎么解决?

比如你读到了sdr里面
SqlDataReader sdr=cmd.ExecuteReader();
while(sdr.Read())
{
textbox1.text=sdr[0].ToString(),
textbox2.text=sdr[1].ToString(),
textbox3.text=sdr[2].ToString(),
}

protected void Form1_Load(object sender, EventArgs e)
{
myconnection.Open();
//连接打开,声明SqlConnection myconnection = new SqlConnection
//("DataSource=..........");
SqlDataAdapter myadapter=new SqlDataAdapter(str_select, myconnection);
//数据适配器,str_select="select id,name,age from table1 where..."。

myset.Clear();//清空数据集DataSet myset = new DataSet();

myadapter.Fill(myset,"table1");//参数为数据集对象和表明
myconnection.Close();//连接关闭
this.textbox1.text= myset.Tables[0].Rows[0][0].ToString();
this.textbox2.text= myset.Tables[0].Rows[0][1].ToString();
this.textbox3.text= myset.Tables[0].Rows[0][2].ToString();