How to create a MS Access File

G

Guest

I search in all the Disscussion but can not found.

How can I create a MS Access Database file using C# code with a given Table
Structure ?

For example, I want to create a Access Database File with 2 tables: Student
and Department, of course, using C# code, not using MS Access to create
manually.

In Department table, I will have 3 Columns: DepartmentID (primary key, auto
increement), DepartmentName (Unique) and DepartmentDescription.

In Student Table I will have 4 columns: StudentID (primary key,
auto-increement), StudentName, DepartmentID (foreign key), and
StudentBirthday (date type).

Is there anyone could help me ?
 
L

Lenard Gunda

How can I create a MS Access Database file using C# code with a given Table
Structure ?

After you have a ADO.NET connection to an MDB file you can use SQL
commands and a Command object to create the structure (CREATE TABLE,
etc). However, I found no way to create the MDB file itself otherwise
than creating an empty one in Access.

Launch Access, create new blank database (MDB file), save, exit. You can
then connect to this file from C#. I think you could also keep the empty
file and duplicate that when you need new databases.

-Lenard
 
G

Guest

Oh, I think that there is a way to create MS Access database file by using C#
code only. Because Last month I had use a trial program that create MS Access
Database file without MS Access Installed in my computer.

Is there any one can help me ?
 
H

Hans Kesting

Oh, I think that there is a way to create MS Access database file by using C#
code only. Because Last month I had use a trial program that create MS Access
Database file without MS Access Installed in my computer.

Is there any one can help me ?

What I have done when I needed this:
- create an empty MDB with Access (as suggested)
- add it to the project (as embedded resource)
- when I need a new MDB, write that embedded file to the filesystem
So the program did "create" a new mdb file, but without Access-specific
commands.

Hans Kesting
 
G

Guest

Thanks for interesting

But Everytime, if you want your program run correctly, you always must have
an empty MS Access Database file. What's wrong if these database file has
been deleted ? Your program can not run.

My program allows user to create new database everytime they want, and can
run stand-alone.

In the past, my friend had done by this way (using code only to create MS
Database Access file) by using ADOX.CatalogClass.Create() but now I can not
find that code and example to do this.

Thanks you for help me. Again, if you could help me, please help me.
 
G

Guest

I think your solutions sounds OK. But Can you show me how to save Embedded
Resource to File System ?

Thanks in advanced
 
H

Hans Kesting

I think your solutions sounds OK. But Can you show me how to save Embedded
Resource to File System ?

Thanks in advanced

Here is my code:

public void CreateDatabase()
{
const int size = 32768;

// read embedded resource empty.mdb,
// write with new name to binary file
byte[] buffer = new byte[size];
Stream mdbStream =
this.GetType().Assembly.GetManifestResourceStream("<namespace>.Empty.mdb");
// NOTE: replace <namespace>

using (FileStream fs = File.Open(_config.DatabaseFile,
FileMode.OpenOrCreate, FileAccess.Write))
{
while (true)
{
int read = mdbStream.Read (buffer, 0, buffer.Length);
if (read <= 0)
break;
fs.Write(buffer, 0, read);
}
}
mdbStream.Close();
}
 
G

Guest

Oh, It works greate. Again, thank you very much.

Hans Kesting said:
I think your solutions sounds OK. But Can you show me how to save Embedded
Resource to File System ?

Thanks in advanced

Here is my code:

public void CreateDatabase()
{
const int size = 32768;

// read embedded resource empty.mdb,
// write with new name to binary file
byte[] buffer = new byte[size];
Stream mdbStream =
this.GetType().Assembly.GetManifestResourceStream("<namespace>.Empty.mdb");
// NOTE: replace <namespace>

using (FileStream fs = File.Open(_config.DatabaseFile,
FileMode.OpenOrCreate, FileAccess.Write))
{
while (true)
{
int read = mdbStream.Read (buffer, 0, buffer.Length);
if (read <= 0)
break;
fs.Write(buffer, 0, read);
}
}
mdbStream.Close();
}
 
G

Guest

Thank you.

That's all I need to ask.

I have to choose the best way by create DB automatically or using as
Embedded Resource.

Once again, Thank you very much
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top