简单问题复杂化....哪里需要DataSet了?SqlDataAdapter也不需要...SqlConnection objConnection = new SqlConnection(strConnection); SqlCommand Command= new SqlCommand("SELECT yewu FROM tousu WHERE xuhao="+lastName , objConnection ); objConnection.Open(); this.TextBox1.Text=Command.ExecuteScalar().ToString(); objConnection.Close();
TextBox.text = ds.Tables[0].Rows[0][字段名].ToString();
如是DataReader形式如下:
if(Rs.Read())
{
TextBox.text =rs[字段名].ToString();
}
返回的是object形式如下:
TextBox.text = 返回對象.ToString();
用dataset
楼上的也写的差不多了
只不过rows[行序数][字段名].tostring()更通用
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand= new SqlCommand("SELECT * FROM tousu WHERE xuhao="+lastName , objConnection );
DataSet dataset=new DataSet("tousu");
adapter.Fill(dataset);
this.TextBox1.Text=dataset.Tables[0].Rows[0][yewu].ToString();
我这样写,他就报错说c:\inetpub\wwwroot\WebApplication2\WebForm5.aspx.cs(45): 名称“yewu”在类或命名空间“WebApplication2.WebForm5”中不存在
但是我的表里是有这个字段的,为什么啊??
this.DataGrid1.DataBind();
在页面里加上一个DATAGRID控件,再写上面两句话也能显示查询的记录,但是就是在文本框里不知怎么显示
this.TextBox1.Text=dataset.Tables[0].Rows[0]["yewu"].ToString(); //yewu使用双引号
}
this.TextBox1.Text=DataSet.Tables[0].Rows[0]["姓名"].
SqlCommand Command= new SqlCommand("SELECT yewu FROM tousu WHERE xuhao="+lastName , objConnection );
objConnection.Open();
this.TextBox1.Text=Command.ExecuteScalar().ToString();
objConnection.Close();
我也是用这个方法