|
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 ? | |
Share:
|
"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... | | |
> 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 | | |
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 | | |
> 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 | | |
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 | | |
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 | | |
> 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
| | |
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 | | |
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 ? | | |
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 ?
| | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by fripper |
last post: by
|
2 posts
views
Thread by david |
last post: by
|
9 posts
views
Thread by Albretch |
last post: by
|
7 posts
views
Thread by dog |
last post: by
|
1 post
views
Thread by Andrew Chanter |
last post: by
|
9 posts
views
Thread by Marc Miller |
last post: by
|
9 posts
views
Thread by Bob Achgill |
last post: by
|
6 posts
views
Thread by JonSteng |
last post: by
|
6 posts
views
Thread by clusardi2k |
last post: by
| | | | | | | | | | |