
asp,我想在Textbox控件中显示sql语句查询出来的数据,请问怎么实现,下面附了代码
protectedvoidButton1_Click(objectsender,EventArgse){SqlConnectioncon=newSqlConnection...
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(local)\SQLEXPRESS;Initial Catalog=guanlixitong;Integrated Security=True;Pooling=False");
SqlCommand cmd = new SqlCommand("sql", con);
con.Open();
cmd.CommandText = "select sum(weixiufeiyong) from dingdanbiao";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlDataReader dr;
dr = cmd.ExecuteReader();
TextBox1.Text = dr.ToString();
con.Close();
Response.Redirect("dingdanhuizong.aspx"); 展开
{
SqlConnection con = new SqlConnection(@"Data Source=(local)\SQLEXPRESS;Initial Catalog=guanlixitong;Integrated Security=True;Pooling=False");
SqlCommand cmd = new SqlCommand("sql", con);
con.Open();
cmd.CommandText = "select sum(weixiufeiyong) from dingdanbiao";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlDataReader dr;
dr = cmd.ExecuteReader();
TextBox1.Text = dr.ToString();
con.Close();
Response.Redirect("dingdanhuizong.aspx"); 展开
展开全部
因为你这句sql语句查询出来的是一行一列"select sum(weixiufeiyong) from dingdanbiao"所以没有必要使用 SqlDataReader 对象,
可以用 object obj=cmd.ExecuteScalar();
然后 再给TextBox赋值
TextBox1.Text=obj.ToString();
如果非要使用 SqlDataReader 对象 ,也可以
dr = cmd.ExecuteReader();
while(dr.Read())//因为你的SQL语句查询出来只有一行一列所以直接使用下标访问
{
TextBox1.Text = +dr[0].ToString();
}
可以用 object obj=cmd.ExecuteScalar();
然后 再给TextBox赋值
TextBox1.Text=obj.ToString();
如果非要使用 SqlDataReader 对象 ,也可以
dr = cmd.ExecuteReader();
while(dr.Read())//因为你的SQL语句查询出来只有一行一列所以直接使用下标访问
{
TextBox1.Text = +dr[0].ToString();
}
追问
我尝试了您说的第一种方式,能运行,但是点了按钮之后,TextBox1.里面还是没有显示结果。请问是不是哪里出错了,,
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询