Click here to Skip to main content
15,903,540 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I want to create an Access Database at Run time. I used the code below. Although it creates a file, it can not be opened saying that it has unrecognized format. Could you please let me know how I can create an Access Database at Run time?

Thank you very much

string FilePath = FolderPath + "\\myDataBasae.accdb" +;
if (!File.Exists(FilePath))
                OleDbConnection myConnection = new OleDbConnection();
                myConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= "
                                               + FilePath;
                OleDbCommand myCommand = new OleDbCommand();
                myCommand.Connection = myConnection;

                myCommand.CommandText = "CREATE TABLE myTable " +
                                          " (" +
                                            " ID int IDENTITY(1,1) PRIMARY KEY," +
                                            " Day DateTime," +
                                            " Events MEMO" +
                                            " )";
                    myConnection.Open();// This line raises an Exception
                catch (Exception ex)

mslliviu 2-Aug-10 2:37am    
You should detail more the exception message. Maybe you can check the format of connection string, I don't use access but it seems that the string should be something like "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= '\test.accdb'"
Christian Graus 2-Aug-10 23:56pm    
Reason for my vote of 1
1 Vote for asking the same question over and over.

Two alternative options;

  • Create an empty DB using Access and add it to the executable as a resource.
  • Use VBScript to create a new database (save as .vbs and execute)
    Set objConnection = CreateObject("ADOX.Catalog")
    objConnection.Create _
        "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = myDatabeest.mdb"
Share this answer
Christian Graus 2-Aug-10 23:57pm    
Funny, I told him that yesterday, too, in a different thread he started with the same question.
There is more to creating a database file than simply executing a File.Create() statement; this just creates an entry in the directory. Using Access to create a blank database file is probably your best bet, though Eddy's suggestion using VBScript may also work; I've never tried that.
Share this answer
Why on earth do you want to create db on-the-fly? It simply is over complicating the whole code. Provide skeleton database with your application. If the database does not exist, simply stop further processing and display useful error message.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900