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

时间:2016-06-30 08:02:34

标签: c# sql-server syntax-error

我收到此语法错误

  

System.Data.SqlClient.SqlException:'('。“

附近的语法不正确

上的

cmd.ExecuteNonQuery(); 

我的类文件中的代码行。

我已经尝试重写整个类文件和我的按钮点击方法但不知何故我仍然做错了。

这是我的代码

Customer.cs:

    //properties
    public int memberId { get; set; }
    public string name { get; set; }
    public string salutation { get; set; }
    public string telNo { get; set; }
    public string eMailAddr { get; set; }
    public string password { get; set; }
    public DateTime birthDate { get; set; }
    public string city { get; set; }
    public string country { get; set; }

    public int add()
    {
        string strConn = ConfigurationManager.ConnectionStrings["NPCSConnectionString"].ToString();

        SqlConnection conn = new SqlConnection(strConn);

        SqlCommand cmd = new SqlCommand("INSERT INTO Member (Name, Salutation, TelNo, EmailAddr, Password, BirthDate, City, Country" 
                                        + "VALUES(@memberID, @name, @salutation, @telNo, @eMailAddr, @password, @birthDate, @city, @country)", conn);

        cmd.Parameters.AddWithValue("@memberID", memberId);
        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("@salutation", salutation);
        cmd.Parameters.AddWithValue("@telNo", telNo);
        cmd.Parameters.AddWithValue("@eMailAddr", eMailAddr);
        cmd.Parameters.AddWithValue("@password", password);
        cmd.Parameters.AddWithValue("@birthDate", birthDate);
        cmd.Parameters.AddWithValue("@city", city);
        cmd.Parameters.AddWithValue("@country", country);

        conn.Open();

        cmd.ExecuteNonQuery();

        conn.Close();
        return 0;
    }

以下代码用于调用Web表单中的add()方法

            Customer objCustomer = new Customer();

            objCustomer.memberId = 8;
            objCustomer.name = txtName.Text;
            objCustomer.salutation = ddlSalutation.SelectedItem.Value;
            objCustomer.telNo = txtTelNo.Text;
            objCustomer.eMailAddr = txtEmail.Text;
            objCustomer.password = txtPassword.Text;
            objCustomer.birthDate = calBirthdate.SelectedDate;
            objCustomer.city = txtCity.Text;
            objCustomer.country = ddlCountry.SelectedItem.Value;

            int errorCode = objCustomer.add();

            if (errorCode == 0)
            {
                Response.Redirect("SuRegister.aspx");
            }

3 个答案:

答案 0 :(得分:3)

在插入值中,您传递了一个名为@memberID的参数,但是您没有在insert语句中包含它,并且您缺少右括号")"。请尝试以下方法:

"INSERT INTO Member (MemberId, Name, Salutation, TelNo, EmailAddr, Password, BirthDate, City, Country)" + "VALUES(@memberID, @name, @salutation, @telNo, @eMailAddr, @password, @birthDate, @city, @country)"

答案 1 :(得分:2)

末尾缺少)
INSERT INTO Member (Name, Salutation, TelNo, EmailAddr, Password, BirthDate, City, Country

答案 2 :(得分:2)

您在查询中错过了一个)

SqlCommand cmd = new SqlCommand("INSERT INTO Member (Name, Salutation, TelNo, EmailAddr, Password, BirthDate, City, Country)" 
                                        + "VALUES(@memberID, @name, @salutation, @telNo, @eMailAddr, @password, @birthDate, @city, @country)", conn);
相关问题