简体   繁体   中英

C# When creating a table I get “incorrect syntax” error if my column / field name contains a “-” hyphen

Below is a snippet of my code, when a table name contains a hyphen, I get the error below. How can I fix this? Thanks for the help.

alt text http://img109.imageshack.us/img109/148/createtable.png

ex = {"ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near '-'."}

Use [] round the column name:

CREATE TABLE [test2] 
(cn VarChar(1024) NULL,
 [tutor-id] VarChar(1024) NULL)

Or preferably stick to column names which don't require special treatment...

Note that it's a column name which has a hyphen, not the table name.

使用[tutor-id]

如果必须,您可以通过将字段名称设置为[tutor-id]而不是tutor-id来解决此问题,但只是将字段重命名为tutorId是一种我宁愿采用的方法。

SQL doesn't like hyphens. Try enclosing tutor-id in square brackets: [tutor-id]

您可以用下划线替换连字符,或将名称括在[和]中。

Try:

CREATE Table [test2] ( [cn] varchar(1024) NULL, [tutor-id] varchar(1024) NULL )

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