我得到以下錯誤,我一直在試圖找出幾個星期的問題是什麼,但沒有,有人可以幫忙嗎?ExecuteNonQuery:不正確的語法附近/
'System.Data.SqlClient.SqlException'
類型的未處理的異常出現在System.Data.dll
附加信息:近
'/'
不正確的語法。
這裏是代碼
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 WindowsFormsApplication4
{
public partial class Add_Position : Form
{
SqlCommand cmd;
SqlConnection con;
SqlDataAdapter da;
public Add_Position()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'position_ListDataSet.Table' table. You can move, or remove it, as needed.
this.tableTableAdapter.Fill(this.position_ListDataSet.Table);
}
private void button1_Click(object sender, EventArgs e)
{
con=new SqlConnection(@"Data Source=sqlserver;Initial Catalog=Position_List;Integrated Security=False;User ID=administrator;Password=;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
con.Open();
cmd = new SqlCommand("INSERT INTO Table (Name, Dwt, Built, Position, Area, L/C from, L/C to, Contact) Values (@Name, @Dwt, @Built, @Position, @Area, @L/C from, @L/C to, @Contact)", con);
cmd.Parameters.Add("@Name", nametxt.Text);
cmd.Parameters.Add("@Dwt", dwttxt.Text);
cmd.Parameters.Add("@Built", builttxt.Text);
cmd.Parameters.Add("@Position", postxt.Text);
cmd.Parameters.Add("@Area", areatxt.Text);
cmd.Parameters.Add("@L/C from", lcftxt.Text);
cmd.Parameters.Add("@L/C to", lcttxt.Text);
cmd.Parameters.Add("@Contact", contxt.Text);
cmd.ExecuteNonQuery();
}
}
感謝
錯誤消息告訴你什麼是錯誤的 - 「L/C from」需要轉義以指定爲列名稱。就個人而言,我建議重命名您的列,以避免空格,標點符號和關鍵字。另外,請注意,您已經在這裏指定了一大堆不相關的代碼(所有這些空的事件處理程序,以開始)...將來,請嘗試生成[mcve]。 –
您不能使用斜槓或空格命名列而不會將其轉義。理想情況下,您不應該根本不需要轉義列名,所以更改這些列的名稱,具體而言,應將兩列「L/C from」和「L/C to」重命名爲不含斜槓或空格。即使只是'L_C_from'雖然還不算好,但L和C代表什麼?這些信件背後隱藏着一個更好的名字,請記住,您不必保存輸入內容,以便明確地使用這些名稱。 –
我建議將你的列名和參數名改爲有意義的,不包含「\」字符。閱讀:[SQL查詢中\(反斜槓)的含義是什麼?](http://stackoverflow.com/q/15214509/447156) –