Incorrect syntax near the keyword 'User'

前端 未结 2 1744
春和景丽
春和景丽 2020-12-11 23:42

The error is:

An unhandled exception of type \'System.Data.SqlClient.SqlException\' occurred in System.Data.dll
Additional information: Incorrec

相关标签:
2条回答
  • 2020-12-12 00:05

    "User" is a reserved word in SQL Server, so you have to use a delimited identifier to refer to your table. Try

    SqlCommand command4 = new SqlCommand("SELECT * FROM [User]", conn);
    

    instead... or rename the table to something which isn't reserved.

    (I'd also strongly advise you to keep the data access out of your UI code, dispose of connections properly etc... but that's a different matter.)

    0 讨论(0)
  • 2020-12-12 00:19

    User is a built-in function in SQL Server. You need to surround the name with square brackets: [User]. This goes for all table names and other user-defined names that happen to collide with keywords, reserved words or built-in names, so I suspect you will need to write [Order] as well, since ORDER is an SQL keyword.

    0 讨论(0)
提交回复
热议问题