做了一个用户名检测程序,检测出错,无论用户名存在不存在都检测不出来,高手帮我看看那里错了 ???
类代码:
public static bool judge(string userName)
{
bool blf;
SqlConnection con=new SqlConnection("server=.;database=login;uid=sa;pwd=;");
con.Open();
string strSQL = "select count(*) from [user] where userName='" + userName + "'";
SqlCommand cmd = new SqlCommand(strSQL, con);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
{
blf = false;
}
else
{
blf = true;
}
return blf;
}
自定义控件代码:
protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
{
string userName = this.TextBox1.Text;
if (db.judge(userName))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}