C#SQL语法错误

Top*_*her 0 c# sql sql-server syntax

任何人都可以告诉我为什么当我尝试将字段添加到此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();
        }
Run Code Online (Sandbox Code Playgroud)

Son*_*nül 5

Table并且Time是T-SQL 中的保留关键字.你需要使用方括号,如[Table][Time].

作为最佳实践,将它们更改为非保留字.

Date在SQL Server的未来版本中也可以是保留关键字.您可能还需要使用方括号.