Create accdb or mdb from code

StewartBW 1,145 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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 85,881 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 32,376 Reputation points Microsoft Vendor
    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.