How to create Microsoft Access database in C# programmatically?
The simplest answer is to embed an empty .mdb
/ .accdb
file in your program and write it out to disk.
The correct answer is to use COM Interop with the ADOX library:
var cat = new ADOX.Catalog()cat.Create(connectionString);
Remember to generate your connection strings using OleDbConnectionStringBuilder
.
Try:
using ADOX; //Requires Microsoft ADO Ext. 2.8 for DDL and Securityusing 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
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.
It is also found as a reference:
which is included as ADOX in the references
By default, columns are created as text[255]. Here are a few examples to create columns as different datatypes.
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