System.Data.SqlClient.SqlException:'userX'附近的语法不正确

时间:2019-07-18 16:07:22

标签: c# sql asp.net visual-c++ webforms

当我尝试登录时尝试创建登录页面时,出现此错误。

不知道如何解决。

有什么想法吗?

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistroConnectionString"].ConnectionString);
        conn.Open();
        string checkuser = " select count(*) from NewLogins where username = '" + TextBoxUsername.Text + "' and Password = '" + TextBoxPassword.Text + "'";
        SqlCommand com = new SqlCommand(checkuser, conn);
        int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
        conn.Close();
        if (temp == 1)
        {
            conn.Open();
            string checkPasswordQuery = "select password from NewLogins where username'" + TextBoxUsername.Text + "'";
            SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn);
            string password = passComm.ExecuteScalar().ToString().Replace(" ", "");
            conn.Close();
            if (password == TextBoxPassword.Text)
            {
                Session["New"] = TextBoxUsername.Text;
                Response.Write("Password is correct");
                Response.Redirect("Manager.aspx");
            }
            else
            {
                Response.Write("Password is not correct");
            }
        }
        else
        {
            Response.Write("Username is not correct");
        }

    }

这是我得到的错误:

System.Data.SqlClient.SqlException:“'userX'附近的语法不正确

此行的字符串密码= passComm.ExecuteScalar()。ToString()。Replace(“”,“”);

1 个答案:

答案 0 :(得分:0)

string checkPasswordQuery = "select password from NewLogins where username='" + TextBoxUsername.Text + "'";
如@Igor Tandetnik所述,添加了

“ =” =“运算符