简体   繁体   English

C#SQL语法错误

[英]C# SQL Syntax Error

Can anyone tell me why when I try to add a field into this SQL Server database I get the error 谁能告诉我为什么在尝试向此SQL Server数据库中添加字段时收到错误消息

Incorrect Syntax near the keyword 'Table'. 关键字“表格”附近的语法不正确。

Code: 码:

{
    // TODO: This line of code loads data into the 'tBoxDataSet.Table' table. You can move, or remove it, as needed.
    this.tableTableAdapter.Fill(this.tBoxDataSet.Table);
}

private void button1_Click(object sender, EventArgs e)
{
         SqlConnection cn = new SqlConnection(global::TicketBox.Properties.Settings.Default.TBoxConnectionString);
        try
        {
            using (SqlConnection connect = new SqlConnection(global::TicketBox.Properties.Settings.Default.TBoxConnectionString))
            using (SqlCommand runsql = new SqlCommand(@"INSERT INTO Table (Event_ID,Artist,Venue,Date,Time,Tickets) values(@EventID,@Artist, @Venue, @Date, @Time,@Tickets)", connect))
            {
                runsql.Parameters.Add("@EventID", SqlDbType.Int).Value = textBox1.Text;
                runsql.Parameters.Add("@Artist", SqlDbType.VarChar).Value = textBox2.Text;
                runsql.Parameters.Add("@Venue", SqlDbType.VarChar).Value = textBox3.Text;
                runsql.Parameters.Add("@Date", SqlDbType.Date).Value = textBox4.Text;
                runsql.Parameters.Add("@Time", SqlDbType.Time).Value = textBox5.Text;
                runsql.Parameters.Add("@Tickets", SqlDbType.Int).Value = textBox6.Text;
                connect.Open();
                runsql.ExecuteNonQuery();
            }

            MessageBox.Show("Event Added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.tableTableAdapter.Fill(this.tBoxDataSet.Table);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            cn.Close();
        }

Table and Time are reserved keyword in T-SQL. TableTime是T-SQL中的保留关键字 You need to use them with square brackets like [Table] and [Time] . 您需要将它们与[Table][Time]等方括号一起使用。

As a best practice, change them to non-reserved words. 最佳做法是将其更改为非保留字。

Also Date can be a reserved keyword in future releases of SQL Server. 在将来的SQL Server版本中, Date也可以是保留关键字。 You might need to use it with square brackets as well. 您可能还需要将其与方括号一起使用。

if your table name is TABLE, then.. that is a keyword for sql... 如果您的表名是TABLE,则..这是sql的关键字。

change your query to 将您的查询更改为

INSERT INTO [Table] 插入[表]

using [ ] will override the keyword 使用[]将覆盖关键字

as Tabel is a reserved keyword , you need to use like this [table] 由于Tabel是保留关键字,因此需要像这样[table]使用

change the query to. 将查询更改为。

INSERT INTO [Table] (Event_ID,Artist,Venue,Date,Time,Tickets)  
values(@EventID,@Artist, @Venue, @Date, @Time,@Tickets)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM