在创建文件夹,应用程序数据的SQL Server Compact文件 [英] Create sql server compact file in appdata folder

查看:129
本文介绍了在创建文件夹,应用程序数据的SQL Server Compact文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个简单的软件其中第一和SQL Server Compact 4.在此设置运作的瞬间使用实体框架的代码。实体框架创建的SQL Server Compact文件,如果还没有存在的。到数据库的路径从存储app.config文件内的的ConnectionString中定义的。它是建立这样的:

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>



不过,我想将数据库的当前用户的Application Data文件夹内的文件夹(下在:我的WIN7机器上\Users\User\AppData\Roaming文件夹)。我已经试过的ConnectionString的数据源设定成类似%APPDATA%\Database.sdf,但这并不工作,我得到一个路径中具有非法字符异常。

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.

我要坚持用ConnectionString的方法,因为我想使用不同的数据库为我的单元测试比我的实际应用。这种方式很容易放置一个app.config文件在项目的根来修改数据库。

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?

推荐答案

以下用途:

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>

这篇关于在创建文件夹,应用程序数据的SQL Server Compact文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆