Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new database (.mdb) with ADO.NET

How can I create a empty .mdb file? I'm using ADO.NET and C#. Thanks!

like image 656
Malfist Avatar asked Oct 20 '25 01:10

Malfist


2 Answers

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.

like image 76
MatthewMartin Avatar answered Oct 22 '25 14:10

MatthewMartin


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;
}
like image 45
Tomalak Avatar answered Oct 22 '25 14:10

Tomalak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!