views:

1058

answers:

6

Hi Girls and Guys!

I'm currently working on some evaluation work for a project that I'm planning.

I recently looked at solutions for a data storage mechanism for my application and while researching stumbled upon SQLite. I currently use SQLite with the System.Data.SQLite wrapper.

I really like the way it works but I have one problem with it that I couldn't get fixed and I also found no help concerning my problem on the internet.

I would like my SQLite Database to be embedded into one of my applications DLLs (ie. Title.Storage.dll) to be used within this DLL. Is this possible?

How can I access the database then?

It would be great if I could use something like:

SQLiteConnection con = new SQLiteConnection();
con.ConnectionString="DataSource=Title.Storage.storage.db3";
con.Open();

Thanks in advance and best regards,

3Fox

+6  A: 

An assembly isn't for file storage, it's for code storage. While you can store files in an assembly, they are read only.

Samuel
Resources are definitely read-only from code.
Will
+1  A: 

You can do that by simply embedding file as a resource, but you will need to extract the DB from DLL, after that you can take this file and hot-backup it into SQLite memory db and delete file (or simply open that file). If you will need a details - leave a comment.

Mash
A: 

If you're on NTFS, you can use an alternate data stream. On my project we hide the SQLite database inside another file using an alternate stream called :DB.

Anthony Brien
what happens if I copy the file to a FAT32 partition and back?
Lucas
You will lose alternative stream after copying the file to FAT partition.
mateusza
A: 

SQLite is packaged and distributed as a single C file with a few (3 I think) header files. This means that you can compile the entire 50000 line program with one compile command and get a .o file. From there, you can link it into your application DLL with the other files you link into that DLL.

Once you build sqlite3.o into your application DLL, the symbols in its SQLite's API become available to your programs in the same way that your other C/C++ DLLs become available to your C#/VB programs.

Check out www.sqlite.org/amalgamation.html for more info.

That was totally not what I was trying to achieve. I wanted to embed the <u>database</u> file in a dll not the dll that allows me to access it.
chrischu
A: 

I don't think storing data in DLL is a good idea, but there is a dirty way to do it.

To load data from DLL:

  1. Use System.Reflection.Assembly to load a string from DLL file. (The string is dump of DB)
  2. Create empty SQLite DB in memory.
  3. Use the loaded string as a query to DB to restore its content.

Now you can make any queries in memory.

To save data to DLL:

  1. Dump DB content into a string.
  2. Create temporary file containing SQL-dump wrapped in C# code.
  3. Compile it using "csc" (or "gmcs" in Mono).

It's stupid, but it should work. Hope you will never code that way.

mateusza
+1  A: 

Yes there is a way !!!

Go as per these steps:

1) Carry source code of your app with your exe.. embed all source code in resource file.

2) Embed sqllite db as resource file.

3) extract sqllite db.

4) Do necessary operations on your DB.

5) At runtime using codedom.. compile that source code and produce a exe. with some logic check original exe, DB (may be using file creation time etc..)

6) start a new process and stop original one

7) again compile it using codedom and embed sqllite db as resource.

To stop this going in infinite loop.. check your files time and when to stop in your loop..

.............

There is always a way out !! :D

Happy creating .NET Viruses... LOL

Shivraj