简体   繁体   English

如何以编程方式在C#中创建MS Access表?

[英]How do you create an MS Access table in C# programmatically?

command.CommandText = "CREATE TABLE MyTable (" +
                "[Count] INT NOT NULL AUTO_INCREMENT PRIMARY KEY ," +
                "[TimeAndDate] TIMESTAMP NOT NULL ," +
                "[SerialNumber] VARCHAR( 14 ) NOT NULL ," +
                "[Result] BOOL NOT NULL ," +
                "UNIQUE ([TimeAndDate]))";

command.ExecuteNonQuery();

Code above flags syntax error exception, can you help me correct the query string? 上面的代码标记了语法错误异常,您可以帮助我更正查询字符串吗? Thank you. 谢谢。

I'd suggest pasting the resulting query text into an Access query; 我建议将结果查询文本粘贴到Access查询中。 it will tell you where the error is. 它会告诉您错误在哪里。

On my Access XP, pasting the resulting SQL gave a syntax error on AUTO_INCREMENT; 在我的Access XP上,粘贴生成的SQL会在AUTO_INCREMENT上出现语法错误; it should be AUTOINCREMENT (see Access 2007 SQL data types ) and be specified as a data type, not a constraint. 它应该是AUTOINCREMENT(请参阅Access 2007 SQL数据类型 ),并应指定为数据类型,而不是约束。 BOOL also gave an error => use BIT BOOL也给出了一个错误=>使用BIT

Result which worked: 有效的结果:

CREATE TABLE MyTable (
               [Count] AUTOINCREMENT NOT NULL PRIMARY KEY ,
               [TimeAndDate] TIMESTAMP NOT NULL ,
               [SerialNumber] VARCHAR( 14 ) NOT NULL ,
               [Result] BIT NOT NULL ,
               UNIQUE ([TimeAndDate]));

您需要结尾括号( )

Just after MyTable you use an open bracket "(", that you do not close. 在MyTable之后,您使用了一个不包含方括号“(”)。

To create tables in Access, I normally use ADOX , this prevents this kind of syntax errors. 为了在Access中创建表,我通常使用ADOX ,这可以防止这种语法错误。

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

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