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

Posted on 2004-11-20
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
    LVL 8

    Expert Comment

    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

    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.
    LVL 8

    Expert Comment

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

    Accepted Solution

    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

    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);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now