Embed MS Access Database in C# WinForm app Embed MS Access Database in C# WinForm app database database

Embed MS Access Database in C# WinForm app


It can be done. Simply add it to your project as you would add any other file (right click project -> Add -> Existing Item), then cancel all the dialogs that will popup offering you to handle it for you, then right click your database from your project explorer, go to properties and select Build Action: Embedded Resource.

Then use the method below to dump your database into a temporary file, which you can create by calling Path.GetTempFileName.

internal void CreateBlankDatabase(string destFile){    using (Stream source = new MemoryStream(Properties.Resources.MyEmbeddedDatabase))    using (Stream target = File.Open(destFile, FileMode.Truncate))    {        source.CopyTo(target);    }}

(Note that MyEmbeddedDatabase would be your embedded database name). Then use your temporary file name in your connection string. Make sure you delete your temporary file after you're done. Also, as other said, you won't be able to modify and save any data.


No it shouldn't be done. How would you send someone and update to the .exe file without them losing their data? Keep it separate.

You need to have a way to manage how your applications installs and the file location in your connection string(s). There could be a \Data subfolder in your app folder with the .accdb file(s) in it.


You probably can't achieve what you want with an access database as an embedded resource, but you effectively get the same result by wrapping all your files in another executable app.

When you run the wrapper application, it extracts the "main" C# app, database file, and an updater app (more on this below) to the temporary files folder and runs the main app.

When the main app is closed, it runs the updater app, passing in the paths to the database file and original wrapper application. The updater app updates the wrapper application file with the changed database file. It then finally deletes the database main app and database file from the temp folder. Unfortunately, the updater app can't delete itself, but you could work around that by adding a command to the runonce section of the registry to delete the updater app on the next reboot.


Instead of figuring out how to extract and insert embedded resources, consider having the wrapper application as a compressed, self-extracting executable (like a self-extracting zip or rar file). Here's a codeproject article that describes how to turn a .Net app into a compressed, self extracting exe.