Create sql server compact file in appdata folder - c#

I am developing a simple piece of software which uses Entity Framework code first and sql server compact 4. At the moment this setup works. Entity framework creates the sql server compact file if it doesn't yet exists. The path to the database is defined from within a connectionstring which is stored inside the app.config file. It is build up like this:
<connectionStrings>
<add name="DataContext"
connectionString="Data source=Database.sdf;"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
However, I want to place the database in a folder within the current user's Application Data folder (the C:\Users\User\AppData\Roaming folder on my win7 machine). I've tried setting the Data source of the connectionstring to something like %APPDATA%\Database.sdf, but this doesn't work, I get an "Illegal characters in path" exception.
I want to stick with the connectionstring method, because I'd like to use a different database for my unit tests than with my actual application. This way it is easy to modify the database by placing an app.config file in the root of the project.
Can someone steer me in the right direction?

Use below:
AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
<connectionStrings>
<add name="DataContext"
connectionString="Data source=|DataDirectory|Database.sdf;"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>

connectionString="Data source=Database.sdf;"
This tells your app to look for Database.sdf in your app’s current working directory; which could be anywhere and may not be writeable. You need to look at a location you specify:
connectionString="Data source=|DataDirectory|Database.sdf;"
ADO.NET looks for pipe characters in connection strings and expands them to the value of the property of that name in the application’s domain. So what’s the value of the DataDirectory property? It’s supposed to be set by whatever deployed your application:
.MSI installers set it to the app installation folder. If you allow the user to choose the installation folder, they choose the DataDirectory as well. This is why you should always use |DataDirectory| and never a hard-coded path.
ClickOnce defines a special data folder in your project.
Web apps use the App_Data folder.
The Visual Studio debugger uses the debug folder.
Any Visual Studio files in your project with a “copy to output directory” property will be copied to DataDirectory. In most cases DataDirectory will be a read-only folder. This is fine if your data is read-only, but if you want to write to it you will have to copy your data to a writeable location. Probably the best place is Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData)). There are several ways of doing this:
If you are creating an empty new data file, just use your API’s standard CREATE DATABASE or new SqlCeConnection() or whatever.
If you want to start with a pre-populated seed or starter database, include the seed database in your project . In your application startup, check if the database exists in the SpecialFolder.ApplicationData folder and, if not, copy it there.
If you search the web for sample code on creating local database you will run across a lot of bad advice. Do not do the following:
new SqlCeConnection(#"Data source=c:\users\me\myApp\Database.sdf;"); // Do NOT do this!
I hope I don’t have to explain why hard-coding the path to your data is wrong; but be aware that unless you specify a full path in your connection string, the path is relative to your current working directory.
using (var conn = new SqlCeConnection(#"Data source=|DataDirectory|Database.sdf;"))
{
conn.Open();
// No No No! This throws an Access Exception for Standard users,
// and gets deleted when you repair the app!
var cmd = conn.CreateCommand("INSERT INTO Table (column1, column2) VALUES (#p1, #p2)");
...
}
Do not try to modify the data in DataDirectory. Not only is this directory not always modifiable by users, it is owned by the installer not by the user. Repairing or uninstalling the application will delete all the user’s data; users don’t like that. Instead, copy the installed data to a folder writable by the user and make all changes to the copy.
AppDomain.CurrentDomain.SetData("DataDirectory",
// Wrong, this overwrites where the user installed your app!
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
Do not change the value of DataDirectory in your code, it is set by the installer and if you change it you won’t know where your data was installed. If you are creating an empty database, just open it in your final location. If you are going to make a copy, open the installed database, save it to the user’s location, close the installed database, and open the copy.
I also discourage saving data to Environment.SpecialFolder.CommonApplicationData. This may not be writable by users, and unless there is a very very good reason all users must be allowed to change other users’ data, each user should have their own database.

Related

publish a project with local database

I created a Windows form applicatie with a local database (.mdf) to store and retrieve data from.
the database where I connect to is:
C:\ProgramData\project\Database.mdf
when I publish my project and place my database file in that folder on a other pc and try to run it I get the error unable to locate a local database runtime installation
my connection string is:
conn.ConnectionString = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=""C:\ProgramData\project\Database.mdf"";Integrated Security=True";
so could somebody help me with this problem?
because everything runs fine on my own pc
Did you include the database as "Application File"? If not do the following (at least this is how I am doing it):
Project -> Properties -> Publish -> Application Files
Here set the values for your .mdf and the xx_log.ldf as follows:
Now still in the Publish tab go on Prerequisites. Here you have to check the following depending on what database you are using.
This will download SQL Server Express for the client who is installing your application.
You will also have to change the connection string to a generic path. I suppose the database lies somewhere inside your project folder /bin I guess, not sure anymore. So adjust your connection string to something like:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True
I therfor recommend using a resource file or app.config
But basically i think your problem is that the pc you are installing on does not have SQL Server installed. So just follow the steps above in Prerequisites. The other steps will enable you to deploy the database to the project folder without moving it to a certain folder manually.
I hope this helps.

Unable to update database .MDF is Read Only

I have a local DB that I have in an application. When I install it on another machine I get the error
Unable to update database.. .mdf is read only.
I was able to get around it by editing my permissions on the .mdf and log file themselves. I did some research and noticed that I may want to install the database to a shared folder. However, I am not sure how to do that and the answers I did come across did not make a whole lot of sense to be.
//My connection string
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|AssetDatabase.mdf;Integrated Security=True");
Any guidance would be greatly appreciated. I am learning.
Just go to the program files and find the folder of the installed program.
Just right click the .mdf file in the folder and click PROPERTIES.
In PROPERTIES TAB -> SECURITY -> you'll see Group or Usernames.
In that select the User to which u want to give Access to the file.
If for that user the PERMISSION is not set to FULL CONTROL.
TO CHANGE PERMISSIONS -> CLICK EDIT.
Now the Group or Username box will open.
In that Select The User -> Select FULL CONTROL in Permission Box.
Now follow the steps for the log file too. IF Needed.
OR
Just install your application in different folder other than ProgramFile(x86)
The MDF file would be readonly either because the readonly file attribute flag is set, in which case you have to unset it, or another program has the MDF file locked as readonly. Are you running a version of SQL server that is using that file?
I've never seen a connection to the MDF file directly, usually it's done via the server, and the server will manage all the IO for the MDF file.
E.g. a typical connection string for SQL Server:
Database=<dbname>;Server=<servername>;MultipleActiveResultSets=True;Connection Timeout=10;User Id=<username>;Password=<password>;
Additional links:
1) Failed to update .mdf database because the database is read-only (Windows application)
2) Failed to update database because it is read-only
3) Failed to update database "*.mdf" because read only EntityFramework
4) http://www.codeproject.com/Questions/183758/Failed-to-update-mdf-database-because-the-database
First stop the SQLEXPRESS service from local-Services menu and then try to move and connect the database again by attaching database option in SQLEXPRESS. it should work. it works for me. :)
I solved the same problem as follows:
While creating "Setup", I manually added my database files database.mdf and database_log.ldf into the Application Folder file. But after adding these files, make sure that the ReadOnly option in the Properties section is False.
Also "connectionString" I use:
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;Connect Timeout=30"
just remove the .mdf file location to c:\ or desktop or anywhere but not in
%program file directory
if your database file on the C volume,
try to move the .mfd file to D volume

winforms DataDirectory location

I have a c# winforms app that uses SQL compact and here is the connection string for the DB:
<add name="Test.Properties.Settings.TestManagerConnectionString"
connectionString="Data Source=|DataDirectory|\Database\TestManager.sdf"
providerName="Microsoft.SqlServerCe.Client.3.5" />
How can I get the path that the connection string is pointing to? ie: C:\Users\name\AppData... etc
Why do you want to move it to a different directory? By default a non-administrative user does not have access to make changes in the Program Files directory tree. AppData is a user specific store that (in the case of network hosted profiles) points to the appropriate directory on the network for the user's app data directory.
If you want to share data for multiple users, there is the All_Users Data Directory. I'm not sure how to specify it with the SqlCE connection string though.
Update
Here is the answer to determining the appdata directory.
To set the DataDirectory property, call the AppDomain.SetData method.
If you do not set the DataDirectory property, the following default
rules will be applied to access the database folder:
For applications that are put in a folder on the user's computer,
the database folder uses the application folder.
For applications that are running under ClickOnce, the database folder uses the
specific data folder that is created.

Modify ConnectionString in .EXE.CONFIG file with Windows7?

I have an SQL Server CE 4.0 (SDF) file that under the Users Application Data Folder (because due to Win7 UAC you cannot modify the DB if it is in the \Programs Files\ folder. But the problem remains that I need to now (on install) modifiy the .EXE.CONFIG file (ConnectionString) to point to the new path for the DB, however obviously I do not have access to modify the .EXE.CONFIG file either in the \Program Files\ folder ...
So how do people resolve this issue?
Can you deploy the .EXE.CONFIG file to the users Application Data Folder as well with the SDF file? If not how? I cannot beleive you need to grant special access rights to the \Program Files\Application folder to be able to have full access...
I am using VS2010 and using a standard Deployment Project (MSI) and I don't see anyway to deploy the .EXE in one place the the .EXE.CONFIG in another (can this be done? Is this the right solution?)
Thanks,
Use the %APPDATA% variable in the connection string and you won't have to modify the configuration file. This is also more flexible than hard-coding a path that contains a username, since it will allow all users of the computer to use your program and have their own .sdf files.
However, the environment variables are not automatically expanded in .NET, so you need to enforce it by using the Environment.ExpandEnvironmentVariable method:
// Example appSetting element:
// <add key="examplePath" value="%APPDATA%\example.txt" />
string path = System.Configuration.ConfigurationManager.AppSettings["examplePath"].ToString();
path = Environment.ExpandEnvironmentVariables(path);
System.IO.File.WriteAllText(path, "foobar");

Creating a relative path to a Database in Asp.net for a library

In school I am part of a team of four working to create a GUI to translate the paper records of a made-up company and their functionality to a digital format. We're using an ASP.NET website for this purpose.
Basically we use stored procedures and C# classes to represent the database. The folder we're using for the project contains the site and the libraries in separate folders. If I try to open the site from the folder containing both these elements the site will not run. I want to know if there is some way I can set up a relative path to the database in the Settings.Settings.cs file (or by some other means) of my libraries so I don't have to constantly change the database location for the connection string value every time we move the project.
I suppose I should also mention that the database is in an App_Data folder.
You want to use Server.MapPath(...)
The MapPath method maps the specified relative or virtual path to the
corresponding physical directory on the server.
if your database is in App_Data folder, you should use a connection string like this in web.config:
<add name="Database1ConnectionString1"
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\Database1.mdf;
Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
|DataDirectory| will refer to App_Data, wherever you install your web app.

Categories

Resources