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

时间:2014-10-22 14:17:23

标签: sql-server sqlexception sqlclient

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

我继续收到此错误

  

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

我似乎无法看到错误的位置。

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

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

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 个答案:

答案 0 :(得分:3)

在列列表和)部分的结束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);
            ****

删除它,你应该没事