如果还没有Microsoft Access数据库文件,如何使用C#创建它?
最简单的答案是在程序中嵌入一个空的.mdb
/ .accdb
文件,并将其写入磁盘。
正确的答案是使用ADOX库的COM互操作:
var cat = new ADOX.Catalog()
cat.Create(connectionString);
请记得使用 OleDbConnectionStringBuilder
来生成您的连接字符串。
using ADOX; //Requires Microsoft ADO Ext. 2.8 for DDL and Security
using ADODB;
public bool CreateNewAccessDatabase(string fileName)
{
bool result = false;
ADOX.Catalog cat = new ADOX.Catalog();
ADOX.Table table = new ADOX.Table();
//Create the table and it's fields.
table.Name = "Table1";
table.Columns.Append("Field1");
table.Columns.Append("Field2");
try
{
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + "; Jet OLEDB:Engine Type=5");
cat.Tables.Append(table);
//Now Close the database
ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
if (con != null)
con.Close();
result = true;
}
catch (Exception ex)
{
result = false;
}
cat = null;
return result;
}
http://zamirsblog.blogspot.com/2010/11/creating-access-database.html
using ADODB; // 在 .Net Framework 中需要 ADODB
- someone在我的电脑上,Windows 7 sp1 专业版 64 位操作系统中,我发现了 Microsoft ADO Ext. 2.8 for DDL and Security,它位于C:\Program Files\Common Files\System\ado\msadox28.dll。
它也被找到了一个引用:
它作为ADOX被包括在引用中
默认情况下,列被创建为text[255]。以下是一些以不同数据类型创建列的示例。
table.Columns.Append("PartNumber", ADOX.DataTypeEnum.adVarWChar, 6); // text[6]
table.Columns.Append("AnInteger", ADOX.DataTypeEnum.adInteger); // Integer
我找到了这个数据类型列表,可以用于创建和读取Access数据库字段
文本 = adVarWChar
备注 = adLongVarWChar
数字(字节) = adUnsignedTinyInt
数字(整数) = adSmallInt
数字(长整数)= adInteger
数字(单精度)= adSingle
数字(双精度)= adDouble
数字(复制 ID)= adGuid
数字(小数)= adNumeric
日期/时间 = adDate
货币 = adCurrency
自动编号 = adInteger
是/否 = adBoolean
超链接 = adLongVarWChar
// using Microsoft.Office.Interop.Access.Dao;
// using static Microsoft.Office.Interop.Access.Dao.DatabaseTypeEnum;
const string dbLangGeneral = ";LANGID=0x0409;CP=1252;COUNTRY=0";
var engine = new DBEngine();
var dbs = engine.CreateDatabase(@"c:\path\to\database.accdb", dbLangGeneral, dbVersion120);
dbs.Close();
dbs = null;
DatabaseTypeEnum
枚举中的其他值:
dbVersion10
dbVersion11
dbVersion20
dbVersion30
dbVersion40
dbVersion120
dbVersion140
dbVersion150
/passive
和/silent
标志运行64位安装程序;否则,您将收到一个消息,说明无法在以前安装的32位组件上安装64位组件。