繁体   English   中英

C#SQL语法错误

[英]C# SQL Syntax Error

谁能告诉我为什么在尝试向此SQL Server数据库中添加字段时收到错误消息

关键字“表格”附近的语法不正确。

码:

{
    // 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();
        }

TableTime是T-SQL中的保留关键字 您需要将它们与[Table][Time]等方括号一起使用。

最佳做法是将其更改为非保留字。

在将来的SQL Server版本中, Date也可以是保留关键字。 您可能还需要将其与方括号一起使用。

如果您的表名是TABLE,则..这是sql的关键字。

将您的查询更改为

插入[表]

使用[]将覆盖关键字

由于Tabel是保留关键字,因此需要像这样[table]使用

将查询更改为。

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