Friday, April 27, 2018

How to Programmatically Create a Microsoft Access Database File Using C#

You might be wondering what to do when you want to create a database but you do not want manually setting it up and you want just the code to do it for you. Well, you are in the right blog. Without further ado, let me share to you this code snippet.

First, you muss add Microsoft ADO Ext. 6.0 (or whatever version you have) for DDL and Security as a reference.

stringfileName = "YourDatabaseFilename";
ADOX.Catalog cat = new ADOX.Catalog();
ADOX.Table table = new ADOX.Table();
try
{
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ".accdb; Jet OLEDB:Engine Type=5");
}
catch (Exception ex)
{
//For Windows Forms Applications
//MessageBox.Show("Database file is not created.");

//For Console Applications
// Console.WriteLine("Database file is not created.");
 }

No comments:

Post a Comment