如何正确移动.mdf文件的位置并相应地更改连接字符串的DataDirectory [英] How to correctly move location of .mdf file and change Connection String's DataDirectory accordingly

查看:177
本文介绍了如何正确移动.mdf文件的位置并相应地更改连接字符串的DataDirectory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前我的SQL数据库位于

Currently my SQL database is on

C:\Users\Slaven\KasaMP.mdf

我想将其移至我的项目目录[也许是 database文件夹(?)]并在我的目录上进行正确的更改连接字符串。 我的目标是能够使用任何计算机上附加的.mdf文件打开该项目。我当前使用的ConnectionString是由EntityFramework生成的,并且我不确定使此CS指向不同位置的方法是什么。

I want to move it into my projects directory [maybe "database" folder(?)] and make correct changes on my connectionstring. My goal is to be able to open this project with .mdf file attached on any computer. Current ConnectionString I use is EntityFramework generated and I am not sure what is the approach to make this CS point to different location.

ConnectionString:

ConnectionString:

<connectionStrings>
    <add name="KasaMPEntities" connectionString="metadata=res://*/OsnovniPodaci.Model.csdl|res://*/OsnovniPodaci.Model.ssdl|res://*/OsnovniPodaci.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|\KasaMP.mdf;initial catalog=KasaMP;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"
      providerName="System.Data.EntityClient" />
  </connectionStrings>

我的项目路径:

C:\Users\Slaven\Documents\visual studio 2013\Projects\PCKasa\KasaMP

我在其他帖子中发现以下行,但是我不确定它的作用:

I found in other posts this following line but I'm not sure what it does:

AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database"));

我知道我必须将此属性放置在ConnectionString中

I know I have to place this attribute somewhere in the ConnectionString

AttachDbFileName=|DataDirectory|\KasaMP.mdf

有关如何正确执行此操作的任何建议? ^ ^

Any suggestions on how to correctly do this? ^ ^

推荐答案

在WinForms应用程序中,DataDirectory替换字符串指向应用程序启动所在的文件夹。如果是Visual Studio会话,则此文件夹为BIN\DEBUG或BIN\RELEASE文件夹(可能带有x86变体)

In a WinForms application the DataDirectory substitution string point to the folder where the application starts. In case of a Visual Studio session this folder is the BIN\DEBUG or BIN\RELEASE folder (possibly with the x86 variant)

在Visual Studio中运行良好,但您应该知道,在客户PC上且未更改配置设置的情况下,您应该在其中拥有MDF的文件夹与您的应用程序相同。

但是,可悲的是,该位置没有写权限(例如C:\程序文件)。任何数据库应用程序的基本要求。

This works well inside Visual Studio but you should be aware that, in your customer PC and without changing the config setting, the folder where you should have the MDF is the same of your app.
But, sadly this location has no write permissions (like C:\program files). An essential requirement for any database app.

因此,最好的选择是将此文件放在CommonApplicationData文件夹中,您可以使用 Environment.SpecialFolder.CommonApplicationData 枚举
(通常在Windows的最新版本中为C:\PROGRAMDATA。)

So your best bet is to place this file in the CommonApplicationData folder that you can retrieve using Environment.SpecialFolder.CommonApplicationData enum (usually it is C:\PROGRAMDATA in latest version of Windows)

string folder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
string myAppFolder = Path.Combine(folder, "MyReservedAppDataFolder");
Directory.CreateDirectory(myAppFolder);
AppDomain.CurrentDomain.SetData("DataDirectory", myAppFolder);

所有这些操作都应在应用程序中任何与数据访问相关的代码之前完成。
当然,您可以保留设置,而无需进行任何更改。

All this should be done BEFORE any data access related code in your application. Of course you can leave the setting as it is now without making any change.

这篇关于如何正确移动.mdf文件的位置并相应地更改连接字符串的DataDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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