Closed. This question is not reproducible or was caused by typos。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

4年前关闭。



Improve this question




我收到此语法错误




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");
            }

最佳答案

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

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

关于c# - System.Data.SqlClient.SqlException : Incorrect syntax near '(' [closed],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38117281/

10-11 02:01