Create accdb or mdb from code

StewartBW 1,805 Reputation points
2024-05-16T12:30:38.7166667+00:00

Hello

I can create a new xls or xlsx Excel file with this:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\x.xls;Extended Properties=""Excel 8.0"""
Using ExcelConnection As New OleDbConnection(AboveString)

But how to create a new mdb or accdb empty Access database file?

I'm using Access Database Engine Runtime. Also if I add ;HDR=YES to the end of string, I see no difference in header's first row?

Thanks.

Developer technologies VB
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,681 Reputation points
    2024-05-16T13:25:25.1666667+00:00

    A way is with ADOX

    A test :

                    // Add reference to : Microsoft ADO Ext. 6.0 for DDL and Security
    
                    ADOX.Catalog cat = new ADOX.Catalog();
                    try
                    {
                        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\newdb.mdb;Jet OLEDB:Engine Type=5");
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("Database creation error : " + ex.Message, "Error");
                    }
    
    

  2. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2024-05-17T07:17:21.9233333+00:00

    Hi @StewartBW ,

    .NET Framework does not provide a direct method for creating Access databases natively.

    But you can copy and rename programmatically using a template database file.

    Then use SQL DDL (Data Definition Language) commands to create tables and structure within the new database file.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.