Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#
Tip/Trick

Create a blank Jet database

Rate me:
Please Sign up or sign in to vote.
4.50/5 (8 votes)
13 Nov 2010CPOL1 min read 35.7K   9   6
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;
try
{
  System.Reflection.Assembly objAssembly =
      System.Reflection.Assembly.GetExecutingAssembly();
  objStream =
      objAssembly.GetManifestResourceStream("MyNameSpace.blankjetdb.mdb");
  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);
  objFileStream.Close();
}
catch (Exception e)
{
  MessageBox.Show("Database not created: "+e.Message);
}
finally
{
  if (objFileStream != null)
  {
    objFileStream.Close();
    objFileStream = null;
  }
}

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

License

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


Written By
Engineer Comport Computing
United States United States
I am a software developer specializing in technical and numerical software systems and also a PhD Petroleum Engineer.
I started programming in IBM 1620 machine code in 1967, then FORTAN on CDC mainframes mainframes in 1970. I later used ALGOL, BASIC, FORTH, Pascal,Prolog, C, F#, C#, etc.
I generally use whatever language available thatallows me to accomplish what is neccesary.

Comments and Discussions

 
GeneralReason for my vote of 5 Thanks for your effort Pin
Member 43208444-Jul-11 13:10
Member 43208444-Jul-11 13:10 

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

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