簡體   English   中英

發生 SQLException: ' ' 附近的語法不正確

[英]SQLException Occured: Incorrect syntax near ' '

我得到

SqlException :nvarchar 附近的語法不正確
“ID”附近的語法不正確

在我的代碼中。 請問有人可以幫我解決嗎?

我的代碼是

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
   partial class Form2 : Form
   {
       public Form2()
       {
           InitializeComponent();
       }

       SqlCommand cmd;
       SqlConnection con = new SqlConnection(@"Data Source=HAIER-PC;Initial Catalog=PDCS;Integrated Security=True");
       SqlDataAdapter SDA;

       private void button3_Click(object sender, EventArgs e)
       {
            con.Open();
            cmd = new SqlCommand("INSERT INTO CusUtil(Customer ID, Age, Experience, Preferred Alternatives, Outer Shell, Base Gasket, Vent, Vent Type, Impact Absorbent Liner, Eyeport Gasket, Face Shield, Comfort Liner, Chin Strap, Weight, Estimated Price)
                                  VALUES(@Customer ID, @Age, @Experience, @Preferred Alternatives, @Outer Shell, @Base Gasket, @Vent, @Vent Type, @Impact Absorbent Liner, @Eyeport Gasket, @Face Shield, @Comfort Liner, @Chin Strap, @Weight, @Estimated Price)", con);
           cmd.Parameters.Add("@Customer ID", textBox1.Text);
           cmd.Parameters.Add("@Age", comboBox1.SelectedItem.ToString());
           cmd.Parameters.Add("@Experience", comboBox2.SelectedItem.ToString());
           cmd.Parameters.Add("@Preferred Alternatives", comboBox3.SelectedItem.ToString());
           cmd.Parameters.Add("@Outer Shell", textBox2.Text);
           cmd.Parameters.Add("@Base Gasket", textBox3.Text);
           cmd.Parameters.Add("@Vent", textBox4.Text);
           cmd.Parameters.Add("@Vent Type", textBox5.Text);
           cmd.Parameters.Add("@Impact Absorbent Liner", textBox6.Text);
           cmd.Parameters.Add("@Eyeport Gasket", textBox7.Text);
           cmd.Parameters.Add("@Face Shield",textBox8.Text);
           cmd.Parameters.Add("@Comfort Liner",textBox9.Text);
           cmd.Parameters.Add("@Chin Strap",textBox10.Text);
           cmd.Parameters.Add("@Weight",textBox11.Text);
           cmd.Parameters.Add("@Estimated Price",textBox12.Text);

           cmd.ExecuteNonQuery();
           con.Close();
       }
    }
}

錯誤發生在ExecuteNonQuery 該代碼只是將數據保存到 SQL Server 數據庫中。

在mysql中,允許在列名中使用空格,但列名必須用反引號包裹..所以只是為了讓用戶看到帶有反引號的命令

但正如 Uuerdo 所建議的 .. 不應該使用參數中的空格...嘗試使用下划線(或駝峰式大小寫)

`

 cmd = new SqlCommand("INSERT INTO CusUtil(`Customer ID`, 
        Age,Experience,`Preferred Alternatives`,`Outer Shell`,`Base 
        Gasket`,Vent,`Vent Type`,`Impact Absorbent Liner`,`Eyeport Gasket`
        ,`Face Shield`,`Comfort Liner`,`Chin Strap`,Weight,`Estimated 
        Price`)VALUES(@Customer_ID,@Age,@Experience,@Preferred_Alternatives,
     @Outer_Shell,@Base_Gasket,@Vent,@Vent_Type,@Impact_Absorbent_Liner,
      @Eyeport_Gasket,@Face_Shield,@Comfort_Liner,
   @Chin_Strap,@Weight,@Estimated_Price)",con);

在 SQLSERVER 中使用 [] 而不是 ``

能否請您添加 [ ] - 帶有空格的列的方括號並刪除參數上的空格。 這會有所幫助,因為這一切都取決於您在 mySQL 上設置的模式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM