急急急!!在线等,c#如何把数据库查询语句的值读出来赋给变量
数据库查询语句为:
string strsql6 = "select count(sno) from student where cno='" + class1 + "' and cet4>425";
想把查询后的值赋给TextBox;
[解决办法]
Sqlcommond cmd =new Sqlcommond();textBox.Text=cmd.ExecuteScalar().ToString();
[解决办法]
using System.Data.SqlClient;using(SqlConnection con = new SqlConnection("Data Source=.;uid=sa;pwd=sa;Database=Northwind")){ con.Open(); string strsql6 = "select count(sno) from student where cno='" + class1 + "' and cet4>425"; SqlCommand cmd = new SqlCommand(strsql6,con); this.TextBox1.Text = cmd.ExecuteScalar().ToString();}
[解决办法]
using System.Data.SqlClient;using(SqlConnection con = new SqlConnection("Data Source=.;initial catalog=Northwind;user id=sa;password=sa")){ string strsql6 = "select count(sno) from student where cno=@class1 and cet4>425"; using(SqlCommand cmd = new SqlCommand(strsql6,con)) { con.Open(); cmd.Parameters.AddWithValue("@class1",class1); this.TextBox1.Text = cmd.ExecuteScalar().ToString(); }}