Click here to Skip to main content
Click here to Skip to main content

Tagged as

Create a blank Jet database

, 13 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
How to create a blank Jet database

I recently needed to be able to create a Jet (Microsoft Access) database programatically from a C# WinForms application. Although the rest of the application uses standard SQL with System.Data.OleDB, for Jet databases, there is no method for creating a new database.

To solve this problem, I manually created a new Jet database using Microsoft Access.TM I simply opened Access, then created a new database from New | Blank Database from the Office button menu. For my purpose, I selected an Access 2000 format, but you can use whatever format makes sense. I named the database blankjetdb.mdb, but any convenient name is fine. Once created, I quit Access, and copied the blankjetdb.mdb to my project directory.

Using Visual Studio 2008TM, I opened the project that needs to create a blank database and add the blankjetdb.mdb file to the project, set Build Action to Embedded Resource and Copy to Output Directory to Copy Always. Now, whenever the project is built, blankjetdb.mdb will be embedded as a resource in the project. This adds only about 170 kB to the project in my case.

To create a Jet database in C#, the following code can be used to extract the blank database and save it to disk as required. Change the string "MyNameSpace.blankjetdb.mdb" to use your own application namespace and file name, of course.

  // Extract DB from resources
  Stream objStream = null;
  FileStream objFileStream = null;
    System.Reflection.Assembly objAssembly = 
    objStream =
    byte[] abytResource = new Byte[objStream.Length];
    objStream.Read(abytResource, 0, (int)objStream.Length);
    objFileStream = new FileStream(Filename, FileMode.Create);
    objFileStream.Write(abytResource, 0, (int)objStream.Length);
  catch (Exception e)
    MessageBox.Show("Database not created: "+e.Message);
    if (objFileStream != null)
      objFileStream = null;

Once the database file has been created, it can be opened using the OleDbConnection class with a suitable connection string.


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


About the Author

Walt Fair, Jr.
Engineer Comport Computing
United States United States
Walt has been playing with software since around 1967 and has generated more runtime errors than the average village idiot. He is a CEO, Petroleum Engineer, software consultant, janitor, and now a graduate student again. Rather than sleep, he also plays with algorithms and systems for technical computing, develops software for engineering evaluations and is an avid amateur radio operator.
Walt was admitted back to UT Austin and is actually attempting to complete a PhD in engineering, thereby proving that he is crazier than the average old fart.
And now UT has gone and admitted Walt to PhD candidacy,now my disertation has been submitted and it is underreview, so as soon as it has been reviewed, I can wrtap things up and graduate. proving that old guys can still ... what was he doing again?

Comments and Discussions

GeneralThank you so much dear walf Pinmembermonstrous programmer18-Mar-12 3:46 
GeneralReason for my vote of 5 Thanks for your effort PinmemberMember 43208444-Jul-11 14:10 
GeneralReason for my vote of 5 Excellent resource - I spent years s... Pinmemberyearling210010-Jan-11 1:35 
GeneralReason for my vote of 1 1 PinmemberJFergulbops12-Nov-10 2:18 
GeneralThanks Walt. PinmemberSlacker00710-Nov-10 6:07 
Thanks Walt.
GeneralReason for my vote of 5 Nice workaround to avoid the use of ... PinmemberMark Lemke6-Nov-10 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 13 Nov 2010
Article Copyright 2010 by Walt Fair, Jr.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid