asp.net怎么查询数据库某字段总和显示在label里
我知道我现在只是赋值了语句,但是我不清楚怎么把sql查询的结果拿出来给label6显示,求解答SqlConnectionconn=null;try{stringconnS...
我知道我现在只是赋值了语句,但是我不清楚怎么把sql查询的结果拿出来给label6显示,求解答SqlConnection conn = null;try {string connStr = @"Data Source=.;Initial Catalog=LJXYbus;Integrated Security=True;";conn = new SqlConnection(connStr);conn.Open();string sql = "select sum(People) from Convention where YuYueDate='" + TextBox1.Text + "'and Time='" + DropDownList1.Text.ToString() + "'";SqlCommand cmd = new SqlCommand(sql, conn);SqlDataReader reader = cmd.ExecuteReader();if (reader.Read()) {Label6.Text =sql;}
展开
展开全部
给你写个最简单的例子比如说页面上有三个TextBox控件!
//连接字符串
string ConString = ConfigurationManager.ConnectionStrings["DemoConnectionString"].ConnectionString.ToString();
protected void Page_Load(object sender, EventArgs e)
{
string sql = "select username,password,sex from TestDB wher id=1";//sql语句
SqlConnection con = new SqlConnection(ConString);
con.Open();//打开连接
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
TextBox1.Text = dr["username"].ToString();//texbox1的值
TextBox2.Text= dr["password"].ToString();//texbox2的值
TextBox3.Text= dr["sex"].ToString();//texbox3的值
}
con.Close();//关闭连接
}
注意:不管你是想获取那个字段的值,sql语句总重要!
//连接字符串
string ConString = ConfigurationManager.ConnectionStrings["DemoConnectionString"].ConnectionString.ToString();
protected void Page_Load(object sender, EventArgs e)
{
string sql = "select username,password,sex from TestDB wher id=1";//sql语句
SqlConnection con = new SqlConnection(ConString);
con.Open();//打开连接
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
TextBox1.Text = dr["username"].ToString();//texbox1的值
TextBox2.Text= dr["password"].ToString();//texbox2的值
TextBox3.Text= dr["sex"].ToString();//texbox3的值
}
con.Close();//关闭连接
}
注意:不管你是想获取那个字段的值,sql语句总重要!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询