简体   繁体   English

C#创建表时,如果我的列/字段名称包含“ - ”连字符,则会出现“语法不正确”错误

[英]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 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 '-'."} ex = {“错误[42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]第1行:' - '附近的语法不正确。”}

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. SQL不喜欢连字符。 Try enclosing tutor-id in square brackets: [tutor-id] 尝试将tutor-id括在方括号中: [tutor-id]

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

Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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