Embed database to application in C#/VB.net - c#

Can we embed database to application in C#.net/vb.net?

You can use serverless RDBMS such as SQL Server Compact or SQLite.

Sure if you put SQLite file as a resource (embedded) you could then run that file in memory stream (I think) of write it out as a file on runtime and delete it when closing down.
.NET SQLite Wrapper/dll

Yes. You can use SqlLite or VistaDB and store the db file as an assembly reource and extract the db file upon first run in the same directory where primary executable is residing.

If your planning of using Sqlite and you should, I recommend you use this provider http://sqlite.phxsoftware.com/ (it's open source) with good support from an active community.

You can use Firebird Embedded. It is free software.
There are .NET Data Provider for it.

Related

Prepare code to programmatically create SQLite database - exporting template

In Microsoft Visual Express is there a way to export the SQLite database I've created through the Express interface into a code format so I can have it generated on first install by my customers?
I'd like to take the easiest way to do this without having to manually prepare all the code structure.
I was unable to find any sort of export feature. Any advice?
This resource will help me execute the code once I have it prepared, but I've 12 tables and some of them should come pre-populated, so being able to have the batch code will help.
If it's just an SQLite database you could just publish the file with the rest of your program since it's a normal file without any dependencies.

Load and save .sdf at runtime

I'm building an C#-application that uses an SQL Server Compact 4.0 database (.sdf) with Entity Framework for data. I want to be able to load/save-files from within this application so that the user can load a different database or backup the database to an USB eg.
I know you can create an sdf in code, but how can i load it at runtime (The connectionstring)?
My question is that this must be a common thing to do, what is the best way to go about it? is there any guides out there to do this?
or do you reccomend another way to go about my problem?
//ObjectiveCoder
You should use a SqlCeConnectionStringBuilder to create a connection string containing your file path.

Re-Embedding Sqlite Database file into the same Executable

I'm Creating Win Form application ,I'm adding a Empty Sqlite Database file having Tables in it as embedded data source. on run time i extract Database file into application path and INSERT THE VALUES into the TABLE of that Database file.
On Closing the application again i have to update or replace Database File into Executable.
Is it possible ,if so how to do that.
I'm not sure if I understand the question correctly. If you are trying to re-write to the same exe you are running this is NOT possible. Windows locks code files that are in use so that they can't change. Additionally, it is not advisable either, code and data should be separate.
If you are trying to update another resources executable (that is not currently running), I don't know how to do that programatically (See this thread here for more info How do I replace embedded resources in a .NET assembly programmatically?) but if your program has access to the Visual Studio Compiler tools (which it probably doesn't) you can disassemble and reassemble the executable. See here: http://fortheloveofcode.wordpress.com/2007/09/24/change-resources-inside-assembly/.
Why not just store it in application folder? Or maybe user's AppData if you don't want to show it?

Programmatically Repair SQLite Database

I have a need to try and repair a SQLite database from a .NET program if the database file gets corrupted. I have found several sites such as Fix SQLite and in the FAQ it describes that you can:
Depending how badly your database is corrupted, you may be able to recover some of the data by using the CLI to dump the schema and contents to a file and then recreate.
Does anyone know of a way to repair a SQLite database programmatically in .NET?
You might consider implementing your own strategy for database recovery. You could store backups of the SQLite file and then check that it is OK using:
PRAGMA integrity_check;
If errors are found then you can revert to a backup.
You are overlooking one important word: you can 'recover some data', this is not a repair!
If there is a sitatuation where a corrupted database could be repaired perfectly without user-interaction than it would not be corrupted in the first place and such an repair would have been a standard function of SQLite

Open MS Access with OLEDB connection string and not have access create the .ldb lock file

I have a program in C# that uses an MS Access database and I'm using OleDb to connect and do queries on that database. My issue is that I have some sensitive info in the database and I don't want it to appear as an Access DB. I changed the extension, but when I open it, it still creates the .ldb lock file used by Access. I want to have the DB not create that lock file.
I have read many posts on the issue and it sounds like if I open the DB in Exclusive mode, it will not create that .ldb file. However, so far, I have not found any connection string for OleDb that lets me specify Exclusive access to the DB. The OleDbConnection object in C# has no "Mode" member either, so setting exclusive access that way is out of the question.
If anyone has any connection strings that can open the DB in Exclusive mode, or if anyone knows another way to avoid creating the .ldb lock file in Access, the help would be much appreciated.
http://www.connectionstrings.com/access has an entry for Ecxlusive mode.
I would recommend using SQLite or another non-access option if you want to avoid lock files.
Trying to avoid the lock files is difficult at best. Even if you open the file in exclusive mode, JET creates these files at times.
If you're trying to store sensitive data, and you want to "hide" the type of file, another good option is VistaDB. It's a single file database, but allows full DB encryption. This would probably be a better approach than just trying to mask the fact you're using JET.
You can't really hide that it's an Access database. Anyone can open the file in a hex editor (or even just notepad) and see a string like "Standard Jet DB" (Office 2000/XP/2003) or "Standard ACE DB" (Office 2007) staring right at them. Even if they don't know what that means Google will tell them soon enough. You use could a less-common database, but they will have similar weaknesses.
If you really want security, you're going to have to encrypt the database file and use an engine that will let you keep a decrypted version in memory (IIRC sqlite supports this, or will soon) or use an engine that supports encryption natively. Even then, you can have problems if the ram is paged to disc or if another process "sniffs" your app's ram.
A late update, but my attention was drawn back here today and I wanted to add that just about anything but Access will require you to distribute the engine with the application. You need to also take care that the files for the engine don't give it away as well. Access gets a pass because the engine is already part of windows. You might also try something that's open source, so you can re-compile it into your main application file.
I have several databases with the Exclusive flag set, and I still get .ldb files created each time I open one. If you are really worried about security it's time to move to a 'grown-up' database.
Install SQL Server 2008 Express, use upsize wizard in Access, point to your Express instance.
You could also potentially use Sql Server Compact to do this. It is free and part of Visual Studio. It is actively used by Microsoft in quite a few products, including Windows Live.

Categories

Resources