sql参数化的一个问题
以前都是用sql拼接方式,现在换sql参数方式,向下面这个问题,哪里有错呢?
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string name = TextBox1.Text.Trim();
string sql = "select count(*) from tb_User where UserName=@username";
SqlParameter[] p = new SqlParameter[] {new SqlParameter("@username",TextBox1.Text.Trim()) };
int i =Convert.ToInt32(ExecuteScalar(sql));
if(i>0)
{
Response.Write("<script language=javascript>alert('该用户名已被占用,请重新添加一个!')</script>");
this.TextBox1.Text = "";
this.TextBox1.Focus();
}
}
public int ExecuteScalar(string sql)
{
SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
int i =Convert.ToInt32(cmd.ExecuteScalar());
cn.Close();
return i;
}
------解决方案--------------------
你这个sqlparameter参数没有传到。。。