关于SQL语言的一个问题!
protected void BT1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection( "server=JIALIA;uid=sa;pwd=sa;database=JR_WebDatebase ");
string tet = Textbox1.Text.ToString();
string strSQL = "Select * From Orderform Where [市名]=@tet ";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open();
GridView2.DataSource = cmd.ExecuteReader();
GridView2.DataBind();
Textbox1.Text = " ";
cn.Close();
}
asp.net在编译的时候没有问题在的运行这个Button的时候网页返回 "必须声明变量 '@tet ' "的一个错误。请达人指点一下。
------解决方案--------------------string strSQL = "Select * From Orderform Where [市名]=@tet ";
SqlCommand cmd = new SqlCommand(strSQL, cn);
//添加如下:
dbCmd.Parameters.Add( "@tet ", OleDbType.VarChar);
dbCmd.Parameters[ "@tet "].Value = tet;
或者改成这样:
string strSQL = String.Format( "Select * From Orderform Where [市名]= '{0} ' ",tet);
SqlCommand cmd = new SqlCommand(strSQL, cn);