Creating a new database (.mdb) with ADO.NET
-
19-08-2019 - |
Question
How can I create a empty .mdb file? I'm using ADO.NET and C#. Thanks!
Solution
Copy a pre-existing .mdb file is the best way.
The same is true for most of the other filebased database formats that ADO.NET can connect to, such as Excel files. Since a file based database system is using the filesystem as it's host and API for communication with the outside world (as opposed to say MSSQL which communicates using TCP-IP), it quite natural to use System.IO for actions that in say MS-SQL would be done with T-SQL or system stored procedures or a data specific API that targets thoses (say SMO in SQL server's case).
COPY model.mdb newdb.mdb
is the create DB command
DEL newdb.mdb is the drop
DB command, etc.
OTHER TIPS
I don't think there is a ".NET native" way to do it, but you still can wrap ADOX:
using ADOX; // add a COM reference to "Microsoft ADO Ext. x.x for DDL and Security"
static void CreateMdb(string fileNameWithPath)
{
ADOX.Catalog cat = new ADOX.Catalog();
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Engine Type=5";
cat.Create(String.Format(connstr, fileNameWithPath));
cat = null;
}