Embed a file (.mdb) into app then extract and SaveAs... i.e. stores template in app or DLL file

Posted on 2004-11-20
Medium Priority
Last Modified: 2008-01-09

I have decided that creating a Microsoft Access database was much easier in VB6 than in .NET therefore I have created a blank database (table and relationship structure OK but no data) and would like to:
1) Embed this into the distributable code (either within the main .exe or ideally as a DLL file.
2) Within the application copy the embedded file and SaveAs \my_path\myname.mdb
3) Open and manipulate the database

Hope someone can help. Rather than explaining how to in general terms, a good URL or code example would ensure points.
Thanks in advance,
Question by:nvms
  • 3
  • 2

Expert Comment

ID: 12633313
I think you can do it with embedded resources in .NET, look here for an explanation with images, only think it through in mdb

I think you can use the same technique to get to a stream of an mdb file
mdb_stream = _
           executing_assembly.GetManifestResourceStream(my_namespace _
           + ".my.mdb"
then you can either write that stream to a file \mypath\my.mdb

Author Comment

ID: 12633320
THanks for taking the time to provide this example. The example is in VB not C#, ideally a simple 10 line example of read/write resulting in a new file would be excellent. Will see if any further comments come along otherwise it's yours.

Expert Comment

ID: 12633337
In your case since it's an mdb it's more the principle than the code i think, but here is a c# howto

Accepted Solution

tomvergote earned 500 total points
ID: 12633348
since both use an image and show it instead of saving it, you would have to write the stream to disk
check this article for an extensive explanation

Author Comment

ID: 12633527
Excellent ! I used these starting points and a good example found at:
to create the following test code. Once you choose Project...Add existing item... locate your file (any file, .doc .txt etc) and specify in the properties of the item "Embedded Resource" then the following code works perfectly:
(assumes that there is only one embedded file ending in .mdb, and that the file to create is c:\test.mdb.)
Writes the destination file in 32Kb chunks, my file was 1.6Mb and opened perfectly retaining all table and relationship information. Now all I need to do is keep this code in a DLL file so the overheads in the executable are not too high

Assembly a = Assembly.GetExecutingAssembly();
string [] resNames = a.GetManifestResourceNames();
foreach(string s in resNames)
using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream(s))
using (Stream output = new FileStream("c:\\Test.mdb",
byte[] buffer = new byte[32768];
int read;
while ( (read=input.Read(buffer, 0, buffer.Length)) > 0)
output.Write(buffer, 0, read);

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question