简体   繁体   中英

Incorrect syntax near the keyword 'Order' - Order is column name

I keep getting the following exception:

Incorrect syntax near the keyword 'Order'.

I have a TABLE with columns: Name , Credits , Extension , Order . Order is of type bigint . Now when I go to INSERT a single record into this table, it gives me the above exception.

I have put it in a try/catch block and caught the exception, I have set breakpoints and it does not reveal anything other than the above message.

Can anybody please help shed some light on this? I'm sitting here, scratching my head wondering what the heck is going on and I just can't figure it out. I don't see where I've gone wrong.

        try
        {
            // Insert into database
            sqlconnection = new SqlConnection(@"Data Source=sblah blah blah... intentionally  removed;");

            sqlconnection.Open();

            using (var command = new SqlCommand("Insert Into Images(Name, Credits, Extension, Order) VALUES (@Name, @Credits, @Extension, @Order)", sqlconnection))
            {
                command.CommandTimeout = 240;

                command.Parameters.AddWithValue("Name", workingPicture.Properties.Filename);
                command.Parameters.AddWithValue("Credits", workingPicture.Properties.Credits);
                command.Parameters.AddWithValue("Extension", workingPicture.Properties.Extension);
                command.Parameters.AddWithValue("Order", workingPicture.Properties.Order);

                command.ExecuteNonQuery();

                doneUpdatingDB = true;
            }
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message);
            doneUpdatingDB = false;
        }

The value of Order is 0 .

ORDER is a reserved keyword in SQL Server. You have to enclose is in square brackets if you want to use it as a column name:

"Insert Into Images(Name, Credits, Extension, [Order]) VALUES (@Name, @Credits, @Extension, @Order)"

It is best to avoid using reserved keywords to name your objects (tables, columns, stored procs, etc.).

订单是SQL中的关键字,更改数据库中的字段名称或在订单关键字周围制作[],如“[order]”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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