如何在 C# 中的 MS Access 数据库中创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/776305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:42:07  来源:igfitidea点击:

How to create a table in a MS access database in C#

c#

提问by SyncMaster

I need to create a table in MS access database. Consider, 'ConfigStructure.mdb' being my database name and i need to create a table in this database in C#.

我需要在 MS Access 数据库中创建一个表。考虑一下,'ConfigStructure.mdb' 是我的数据库名称,我需要用 C# 在这个数据库中创建一个表。

How can i do this? I tried with the below code but its not working.

我怎样才能做到这一点?我尝试使用下面的代码,但它不起作用。

        OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + frmMain.strFilePath + "\ConfigStructure.mdb");
        myConnection.Open();
        string strTemp = " KEY Text, VALUE Text ";
        OleDbCommand myCommand = new OleDbCommand();
        myCommand.Connection = myConnection;
        myCommand.CommandText = "CREATE TABLE table1(" + strTemp + ")";
        myCommand.ExecuteNonQuery();
        myCommand.Connection.Close();

This is the error that i get,

这是我得到的错误,

"System.Data.OleDb.OleDbException: Syntax error in field definition
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)\r\n   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)\r\n   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)\r\n   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)"

采纳答案by Julien Poulin

Replace

代替

string strTemp = " KEY Text, VALUE Text ";

with

string strTemp = " [KEY] Text, [VALUE] Text ";

I think the reason for this is that 'KEY' an 'VALUE' are reserved keywords in Access or SQL.

我认为这样做的原因是“KEY”和“VALUE”是 Access 或 SQL 中的保留关键字。

回答by Steve

I suspect the problem is using the word KEY as a column name - this is probably a reserved word. Try using different column names.

我怀疑问题是使用单词 KEY 作为列名 - 这可能是一个保留字。尝试使用不同的列名。

回答by lc.

KEY and VALUE are both reserved words. Particularly it is probably getting hung up on "KEY" because you can specify PRIMARY KEYas a constraint in a CREATE TABLEcommand.

KEY 和 VALUE 都是保留字。特别是它可能会挂在“KEY”上,因为您可以PRIMARY KEYCREATE TABLE命令中指定为约束。

Try using different column names or surrounding them by brackets (e.g. [KEY]) if you reallywant to use them (not suggested).

如果您真的想使用它们(不建议),请尝试使用不同的列名称或用括号括起来(例如 [KEY] )。

回答by Fredrik M?rk

"KEY" and "VALUE" are reserved words in MS Access. If you wish to use those names for your fields you will need to enclose them in square brackets:

“KEY”和“VALUE”是 MS Access 中的保留字。如果您希望将这些名称用于您的字段,您需要将它们括在方括号中:

string strTemp = " [KEY] Text, [VALUE] Text ";

回答by Jim Lahman

On my computer, Windows 7 sp1 Professional 64-bit, I found Microsoft ADO Ext. 2.8 for DDL and Security in C:\Program Files\Common Files\System\ado\msadox28.dll.

在我的计算机 Windows 7 sp1 Professional 64 位上,我找到了 Microsoft ADO Ext。2.8 用于C:\Program Files\Common Files\System\ado\msadox28.dll 中的DDL 和安全

It is also found as a reference:

它也被发现作为参考:

enter image description here

在此处输入图片说明

which is included as ADOXin the references

在参考文献中作为ADOX包含在内

enter image description here

在此处输入图片说明

By default, columns are created as text[255]. Here are a few examples to create columns as different datatypes.

默认情况下,列创建为text[255]。以下是将列创建为不同数据类型的几个示例。

table.Columns.Append("PartNumber", ADOX.DataTypeEnum.adVarWChar, 6); // text[6]
table.Columns.Append("AnInteger", ADOX.DataTypeEnum.adInteger); // Integer 

I found this list of datatypes to create and read access database fields

我找到了这个数据类型列表来创建和读取访问数据库字段

Access Text = adVarWChar

Access Memo = adLongVarWChar

Access Numeric Byte = adUnsignedTinyInt

Access Numeric Integer = adSmallInt

Access Numeric Long Integer = adInteger

Access Numeric Single Precision = adSingle

Access Numeric Double Precision = adDouble

Access Numeric Replicatie-id = adGuid

Access Numeric Decimal = adNumeric

Access Date / Time = adDate

Access Currency = adCurrency

Access AutoNumber = adInteger

Access Yes / No = adBoolean

Access HyperLink = adLongVarWChar

访问文本 = adVarWChar

访问备忘录 = adLongVarWChar

访问数字字节 = adUnsignedTinyInt

访问数字整数 = adSmallInt

访问数字长整数 = adInteger

访问数字单精度 = adSingle

访问数字双精度 = adDouble

访问数字 Replicatie-id = adGuid

访问数字十进制 = adNumeric

访问日期/时间 = adDate

访问货币 = adCurrency

访问自动编号 = adInteger

访问是/否 = adBoolean

访问超链接 = adLongVarWChar

回答by Yura

You should always write names of the tables and columns in these [] Your example:

您应该始终在这些 [] 您的示例中写入表和列的名称:

OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + frmMain.strFilePath + "\ConfigStructure.mdb");
myConnection.Open();
string strTemp = " [KEY] Text, [VALUE] Text ";
OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "CREATE TABLE [table1](" + strTemp + ")";
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();

Works perfect.

工作完美。