Load and save .sdf at runtime - c#

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.

Related

what is advantage of .ini file when set db connect (winform)

I am now involved in a small c# project on winform (just 2-3 select statements),
and the project need to connect to mssql.
I know there are many ways to connect DB.
but why people use .ini or xml file to connect DB?
Is not good insert connect statement(server, id, pw ...) to class?
It is easier to use Configuration file (.ini, .xml) if you want to change connection string later. If you put your data in your code, everytime you change it, you must re-compile your code.

Failed to update database "*.mdf" because read only EntityFramework

I have a C# .NET Framework 4.0 desktop application with Entity Framework as DAL.
When a try to save a data into DBContext on anybodies machine but mine, I recieved an exception
Failed to update database "*.mdf" read only
I keep my DB near .exe file, in folder "DAL/AppData".
How can I allow write access on other machines?
Can I do it programmatically?
I've read that I can place DB into AppRoaming Folder, but this is not my variant.
Thanks in advance.
I keep my DB near .exe file, in folder "DAL/AppData".
Yes. Can it be you mean that this is in the programs file folder, you know.
THe one that windows specs of the last 10 years say is "read only" for normal users?
OUCH.
THer are folders for storing data. There is a SpecialFolders enumeration to get the valid path of every such folder.
How can I allow write access on other machines? Can I do it programmatically?
On a SQL Server, this is done by allowing the other computer to access the server, not the data files. I.e. you connect to the SQL Server on the other machine, which is having the database loaded.
I've read that I can place DB into AppRoaming Folder, but this is not my variant.
First, that would be stupid unless you do actually plan for roaming - SQL shold go into a local folder, never roaming.
Second, "not my variant" is like "Hey, I drive the car against the rules, what can I do not to get speeding tickets". And "following the law is not my variant". Your variant is something WIndows does not care for. Learn how to install your software according to the windows guideline which is VERY clear where changing data should NOT be.

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.

How to restore a SQLite database while an open connection exists?

I am writing a plugin for a C# application which uses SQLite database. The initial database is stored in database.db.default. At any point during runtime the user should be able to reset the database to the default content.
Since the application has always an open connection to the database I can not just copy database.db.default to database.db.
Whether or not it is a good idea to always hold an open connection is a total different question. But that is just how it is since it is not my call to change that part of the application.
Is there any way I am able to restore database.db.default into database.db using C# and .NET 3.0?
I appreciate any kind of help ...
If you can't do a file copy to restore the database then do the restore the hard way. Attach the backup database, delete all the data in the main database, then insert the data from the backup database.
I'm probably missing something here, but can't you just close your connection and reopen it after the copy?

Embed database to application in C#/VB.net

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.

Categories

Resources