I have created an Access (2010, but converted to 2007) database and I am building a front end to it using C#. I can't figure out how to provide the user with the means to create a new instance of that database. The idea is that the user can create multiple databases.
Generally, if I can figure out the concept I can figure out the code to write, but in this case I might need help with the code! Here's what I'm thinking, so if someone can, tell me if this is too klunkity!

When the program is installed, a hidden folder will be created, into which an empty copy of the database will be placed. When the user requests to create a new database, a dialog permits to specify a name and location, and then a copy of the empty database is created with the name, and in the location specified by the user, and a connection string is modified accordingly.

I have a feeling there is a more elegant (efficient) way of doing this, but I can't figure out what it is! It also seems I would have to somehow store multiple connection strings if the user is to be able to open any of the previously created databases.

Recommended Answers

All 4 Replies

Hi there,

I had the same problem and the best solution I found was this:

http://support.microsoft.com/kb/317881

Although once you have created your empty database, created your tables, populated any reference data you will probably find it much easier, if less elegant, to copy an empty database as you suggested. That is what I did!

Regards

Paul

Instead of creating the file separate, you could just add an empty database as a resource into either your exe or a dll and have a method that extracts the stream to the new location. The user will have no idea that your program isn't creating the file from scratch, it won't rely on any apis and its only a couple lines of code.

Thanks for the responses!
Paul, are you saying you used the database copying method also? If so, do you have any code you can share?

Diamonddrake, I think the idea you suggested is a good one too, especially if it's only taking a could lines of code! However, extracting streams is bit beyond my level of expertise at this point, so if you have some code I can study, that would be great.

Thanks, guys!

T

Hi,

Yes I used the database copy method. I stored a database (renamed to .dat so it did not look like a database) within the project. When a user createded a new instance I would perform a simple file copy/rename, thus:

File.Copy(@"C:\projects\template.dat", @"C:\projects\abcd.accdb");

To use the File.Copy you will need the following:

using System.IO;

Let me know if you need anything else.

Regards

Paul

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.