2011-01-24 90 views
14

如何在C#中創建Microsoft Access數據庫文件(如果它不存在)?如何以編程方式在C#中創建Microsoft Access數據庫?

+0

是否有要訪問或者是你尋找一個文件基於數據庫的解決方案存儲數據如果這就是你所需要的,SQLite是很好的和可移植的。 – jlafay 2011-01-25 19:04:13

+0

是的,我現在在Android上使用SQLite。 – 2014-10-23 04:31:54

回答

15

最簡單的答案是在您的程序中嵌入一個空的.mdb/.accdb文件並將其寫入磁盤。

正確的答案是使用COM互操作與ADOX庫:

var cat = new ADOX.Catalog() 
cat.Create(connectionString); 

記住使用OleDbConnectionStringBuilder生成您的連接字符串。

+3

提示:在我的機器 – Matthias 2016-05-15 21:23:29

9

嘗試:

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

9

在我的電腦中,Windows 7 SP1專業版64位,我發現微軟ADO分機。 2.8 for DDL and Security in C:\ Program Files \ Common Files \ System \ ado \ msadox28.dll

還發現作爲參考:

其包括作爲ADOX在參考文獻中

enter image description here

默認情況下,列被創建爲文本[ 255]。以下是一些將列創建爲不同數據類型的示例。

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

我發現數據類型來創建列表和讀取訪問數據庫中的字段

訪問文本= adVarWChar

訪問備註= adLongVarWChar

訪問數字字節= adUnsignedTinyInt

訪問數字整數= adSmallInt

訪問數字長整型= adInteger

訪問數字單精度= adSingle

訪問數字雙精度= adDouble

訪問數字Replicatie-ID = adGuid

訪問數字小數= adNumeric

訪問日期/時間= adDate

訪問貨幣= adCurrency

訪問自動編號= adInteger

訪問是/否= adBoolean

訪問的HyperLink = adLongVarWChar

相關問題