我得到以下错误,我一直在试图找出几个星期的问题是什么,但没有,有人可以帮忙吗?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) –