Link to home
Start Free TrialLog in
Avatar of nvms
nvms

asked on

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

Hi

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,
Stu
Avatar of tomvergote
tomvergote
Flag of United States of America image

I think you can do it with embedded resources in .NET, look here for an explanation with images, only think it through in mdb
http://www.dotnetspider.com/technology/KBPages/442.aspx

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
Avatar of nvms
nvms

ASKER

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.
In your case since it's an mdb it's more the principle than the code i think, but here is a c# howto
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75
ASKER CERTIFIED SOLUTION
Avatar of tomvergote
tomvergote
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nvms

ASKER

Excellent ! I used these starting points and a good example found at:
http://www.dotnet247.com/247reference/msgs/50/253364.aspx
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)
{
if(s.EndsWith(".mdb"))
{
using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream(s))
{
using (Stream output = new FileStream("c:\\Test.mdb",
FileMode.CreateNew,
FileAccess.Write,
FileShare.Write))
{
byte[] buffer = new byte[32768];
int read;
while ( (read=input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
}
}
}