asp.net数据绑定

来源:百度知道 编辑:UC知道 时间:2024/05/21 15:04:38
查询出select name1,name2 from name where id=1 查询出这样的结果,我想在asp.net里分别为两个文本框Text1绑定name1,Text2绑定name2,用DataSet请问如何实现啊,麻烦给个例子

SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);dataset=mydb;uid=sa;pwd=xxx";
string selectString ="select name1,name2 from name where id=1";
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(selectString, con);
DataSet ds = new DataSet();
sda.Fill(ds, "ds");
Text1.text=ds1.Tables["ds"].Rows[0]["name1"].ToString();//因为索引从0开始只有一行当然索引为0:)
Text2.text=ds1.Tables["ds"].Rows[0]["name2"].ToString();
con.close();

我觉得这种单行的东西用dataset太浪费,所以我建议你用如下方法

SqlConnection con = new SqlConnection();
conn.ConnectionString = "server=(local);dataset=mydb;uid=sa;pwd=xxx";
SqlCommand cmd = new SqlCommand("select name1,name2 from name where id=1", conn);
conn.Open();
SqlDataReader dtr = cmd.ExecuteReader(CommandBehavior.Cl