代码之家  ›  专栏  ›  技术社区  ›  Youssef

Sql命令语法错误:System.Data.SqlClient。SqlException:“靠近','的语法不正确。”

  •  0
  • Youssef  · 技术社区  · 7 年前

    我正在用C语言构建一个销售点系统,并通过一个对象将数据从接口传递到数据库。

    private void btnUpdate_Click(object sender, EventArgs e) //UPDATE FUNCTION//
        {
                user_management_system user_mgnt = new user_management_system();
                user_mgnt.Username = txt_userName.Text;
                user_mgnt.Password = txt_password.Text;
                user_mgnt.First_name = txt_firstName.Text;
                user_mgnt.Last_name = txt_lastName.Text;
                user_mgnt.Nationality = txt_nationality.Text;
                user_mgnt.Email = txt_email.Text;
                user_mgnt.Age = (txt_age.Text);
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "UPDATE userlogin SET password = ('"+ user_mgnt.Password + "',first_name='" + user_mgnt.First_name + "',last_name='" + user_mgnt.Last_name + "',age='" + user_mgnt.Age +
                              "',nationality='" + user_mgnt.Nationality + "',email='" + user_mgnt.Email + "', WHERE username ='"+ user_mgnt.Username+ "')";
               cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Data has been successfuly updated");
                displaydata();
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   nb1987    7 年前

    你的前面有一个额外的(错位的)逗号 WHERE 条款它应该是:

    cmd.CommandText = "UPDATE userlogin SET password = ('"+ user_mgnt.Password + "',first_name='" + user_mgnt.First_name + "',last_name='" + user_mgnt.Last_name + "',age='" + user_mgnt.Age +
                              "',nationality='" + user_mgnt.Nationality + "',email='" + user_mgnt.Email + "' WHERE username ='"+ user_mgnt.Username+ "')";
    

        2
  •  -1
  •   mustafa96m    7 年前

    查询文本中存在语法错误,请遵循此表单

    https://www.w3schools.com/sql/sql_update.asp