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

Posted

技术标签:

【中文标题】System.Data.SqlClient.SqlException (0x80131904): \')\' 附近的语法不正确【英文标题】:System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near ')'System.Data.SqlClient.SqlException (0x80131904): ')' 附近的语法不正确 【发布时间】:2014-12-18 00:46:27 【问题描述】:

我一直在尝试调试一段代码,但到目前为止我一直没有成功。

我不断收到这个错误

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

我似乎看不出错误在哪里。

代码如下,查看了该站点上的各种答案并查看了示例。情况是我正在编写一个访问数据库并能够将数据添加和保存到该数据库的程序。它位于服务器上 - 尽管这是在我的笔记本电脑上。

我已经编写、重写和复制了连接字符串,以确保其中没有错误,甚至将字符串的副本放入 word 中,并将其与编码本身中的字符串进行比较,但无济于事。

public partial class AddingClients : Form

    public AddingClients()
    
        InitializeComponent();
    

    String CompanyName2;
    String ClientName2;
    String CompanyAddress12;
    String CompanyAddress22;
    String CompanyAddress32;
    String CompanyTown2;
    String CompanyPostCode2;
    String TelephoneNumber2;
    String CompanyEMail2;
    String CompanyNotes2;
    String ClientReference2;

    public SqlConnection con;

    public void connection()
    
        String connectionstr = @"Data Source=ACER\PATTESTSERVER;Initial Catalog=PatTest;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;";
        con = new SqlConnection(connectionstr);
        con.Open();
    

    private void Finish_Click(object sender, EventArgs e)
    
        ClientReference2 = Id.Text;
        CompanyName2 = CompanyName.Text;
        ClientName2 = ClientName.Text;
        CompanyAddress12 = CompanyAddress1.Text;
        CompanyAddress22 = CompanyAddress2.Text;
        CompanyAddress32 = CompanyAddress3.Text;
        CompanyTown2 = CompanyTown.Text;
        CompanyPostCode2 = CompanyPostCode.Text;
        TelephoneNumber2 = TelephoneNumber.Text;
        CompanyEMail2 = CompanyEMail.Text;
        CompanyNotes2 = CompanyNotes.Text;

        String connectionstr = @"Data Source=ACER\PATTESTSERVER;Initial Catalog=PatTest;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;";
        con = new SqlConnection(connectionstr);
        con.Open();

        SqlCommand cmd = new SqlCommand("INSERT INTO ClientTable(Id, ClientName, CompanyName, CompanyAddress1, CompanyAddress2, CompanyAddress3, CompanyTown, CompanyPostCode, TelephoneNumber, CompanyEMail, CompanyNotes,) VALUES(@parameter1, @parameter2, @parameter3, @parameter4, @parameter5, @parameter6, @parameter7, @parameter8, @parameter9, @parameter10, @parameter11,)", con);

        cmd.Parameters.AddWithValue("@parameter1", ClientReference2);
        cmd.Parameters.AddWithValue("@parameter2", ClientName2);
        cmd.Parameters.AddWithValue("@parameter3", CompanyName2);
        cmd.Parameters.AddWithValue("@parameter4", CompanyAddress12);
        cmd.Parameters.AddWithValue("@parameter5", CompanyAddress22);
        cmd.Parameters.AddWithValue("@parameter6", CompanyAddress32);
        cmd.Parameters.AddWithValue("@parameter7", CompanyTown2);
        cmd.Parameters.AddWithValue("@parameter8", CompanyPostCode2);
        cmd.Parameters.AddWithValue("@parameter9", TelephoneNumber2);
        cmd.Parameters.AddWithValue("@parameter10", CompanyEMail2);
        cmd.Parameters.AddWithValue("@parameter11", CompanyNotes2);

        cmd.ExecuteNonQuery();

        //this method moves to the next screen.
        this.Hide();
        Asset M1 = new Asset();
        M1.Show();
    

    private void SaveandNext_Click(object sender, EventArgs e)

        ClientReference2 = Id.Text;
        CompanyName2 = CompanyName.Text;
        ClientName2 = ClientName.Text;
        CompanyAddress12 = CompanyAddress1.Text;
        CompanyAddress22 = CompanyAddress2.Text;
        CompanyAddress32 = CompanyAddress3.Text;
        CompanyTown2 = CompanyTown.Text;
        CompanyPostCode2 = CompanyPostCode.Text;
        TelephoneNumber2 = TelephoneNumber.Text;
        CompanyEMail2 = CompanyEMail.Text;
        CompanyNotes2 = CompanyNotes.Text;

        String connectionstr = @"Data Source=ACER\PATTESTSERVER;Initial Catalog=PatTest;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;";
        con = new SqlConnection(connectionstr);
        con.Open();

        SqlCommand cmd = new SqlCommand("INSERT INTO ClientTable(Id, ClientName, CompanyName, CompanyAddress1, CompanyAddress2, CompanyAddress3, CompanyTown, CompanyPostCode, TelephoneNumber, CompanyEMail, CompanyNotes,) VALUES(@parameter1, @parameter2, @parameter3, @parameter4, @parameter5, @parameter6, @parameter7, @parameter8, @parameter9, @parameter10, @parameter11,)", con);

        cmd.Parameters.AddWithValue("@parameter1", ClientReference2);
        cmd.Parameters.AddWithValue("@parameter2", ClientName2);
        cmd.Parameters.AddWithValue("@parameter3", CompanyName2);
        cmd.Parameters.AddWithValue("@parameter4", CompanyAddress12);
        cmd.Parameters.AddWithValue("@parameter5", CompanyAddress22);
        cmd.Parameters.AddWithValue("@parameter6", CompanyAddress32);
        cmd.Parameters.AddWithValue("@parameter7", CompanyTown2);
        cmd.Parameters.AddWithValue("@parameter8", CompanyPostCode2);
        cmd.Parameters.AddWithValue("@parameter9", TelephoneNumber2);
        cmd.Parameters.AddWithValue("@parameter10", CompanyEMail2);
        cmd.Parameters.AddWithValue("@parameter11", CompanyNotes2);

        cmd.ExecuteNonQuery();

        this.Hide();
        AddingClients M1 = new AddingClients();
        M1.Show();//Saves the current data then goes to the next record to be tested.

【问题讨论】:

【参考方案1】:

在您的列列表和VALUES 部分的结尾) 之前有一个额外的 逗号:

SqlCommand cmd = new SqlCommand("INSERT INTO ClientTable(Id, ClientName, CompanyName, CompanyAddress1, CompanyAddress2, CompanyAddress3, CompanyTown, CompanyPostCode, TelephoneNumber, CompanyEMail, 

 CompanyNotes,) 
            ****

VALUES(@parameter1, @parameter2, @parameter3, @parameter4, @parameter5, @parameter6, @parameter7, @parameter8, @parameter9, @parameter10, 

 @parameter11,)", con);
            ****

删除它,你应该没问题

【讨论】:

以上是关于System.Data.SqlClient.SqlException (0x80131904): ')' 附近的语法不正确的主要内容,如果未能解决你的问题,请参考以下文章