错误语法不正确

Myw*_*rld 0 c# asp.net

我有注册表单,我在这个表单的用户名文本框中,当我测试网页表单时,我在文本框用户名(Kaz'em)中添加了这个用户,我有这个错误

('em'附近的语法不正确.字符串''后面的未闭合引号.)

public bool RegisteredUser()
{
    bool Return = false;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ElarabyGroup"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Select Count(UserName) From [Registeration] Where [Registeration].UserName = '" + RegisteredUserName + "'", con);
    con.Open();
    if (int.Parse(cmd.ExecuteScalar().ToString()) > 0)
        Return = true;
    con.Close();
    return Return;
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 5

那是因为您直接在SQL中包含用户提供的数据.

不要那样做.

请改用参数化查询,将文本框中的值设置为参数值之一.这样,您可以使查询的"数据"部分远离查询的"代码"部分.

你很幸运,这只是一个语法错误,而不是小Bobby表 ......