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

南楼画角 提交于 2020-01-16 01:04:18

问题


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.


回答1:


I'd suggest pasting the resulting query text into an Access query; it will tell you where the error is.

On my Access XP, pasting the resulting SQL gave a syntax error on AUTO_INCREMENT; it should be AUTOINCREMENT (see Access 2007 SQL data types) and be specified as a data type, not a constraint. BOOL also gave an error => use 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]));



回答2:


You need an ending parenthesis ).




回答3:


Just after MyTable you use an open bracket "(", that you do not close.

To create tables in Access, I normally use ADOX, this prevents this kind of syntax errors.



来源:https://stackoverflow.com/questions/4781323/how-do-you-create-an-ms-access-table-in-c-sharp-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!