By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 2,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

How to create a MS Access File

P: n/a
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 ?
Nov 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Minh" <Mi**@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
Is there anyone could help me ?


I believe that the only way you can do this is with ADOX via InterOp...
Nov 23 '05 #2

P: n/a
> 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
Nov 23 '05 #3

P: n/a
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 ?

"Lenard Gunda" wrote:
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

Nov 23 '05 #4

P: n/a
> 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
Nov 23 '05 #5

P: n/a
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.

"Hans Kesting" wrote:
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

Nov 23 '05 #6

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

Thanks in advanced

"Hans Kesting" wrote:
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

Nov 23 '05 #7

P: n/a
> 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();
}

"Hans Kesting" wrote:
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

Nov 23 '05 #8

P: n/a
Oh, It works greate. Again, thank you very much.

"Hans Kesting" wrote:
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();
}

"Hans Kesting" wrote:
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


Nov 23 '05 #9

P: n/a

Here's a link to creating your DB from nothing

http://groups.google.com/group/micro...f6d8ef62fd67b0

Hope this helps.

-tomas
On Wed, 23 Nov 2005 01:19:11 -0800, "Minh"
<Mi**@discussions.microsoft.com> wrote:
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 ?


Nov 23 '05 #10

P: n/a
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

"Tomas Vera" wrote:

Here's a link to creating your DB from nothing

http://groups.google.com/group/micro...f6d8ef62fd67b0

Hope this helps.

-tomas
On Wed, 23 Nov 2005 01:19:11 -0800, "Minh"
<Mi**@discussions.microsoft.com> wrote:
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 ?


Nov 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.