SQL防爆preSS连接字符串:相对于应用程序位置的MDF文件位置 [英] SQL Express connection string: mdf file location relative to application location
问题描述
我使用SQL防爆preSS数据库在C#单元测试项目的一部分。我的数据库位于:
I am using SQL Express databases as part of a unit test project in c#. My databases is located here:
./Databases/MyUnitTestDB.mdf
我想使用相对路径或变量的app.config
,而不是我的连接字符串定义为:
I would like to use a relative path or variable in the app.config
rather than having my connection string defined as:
AttachDbFilename=C:\blah\blah\blah\yea\yea\yea\MyApplication\Databases\MyUnitTestDB.mdf
我已经看到了使用 | DataDirectory目录|
,但我是纠正想这仅适用于Web应用程序
I have seen the use of |DataDirectory|
but am I correct in thinking this is only applicable to web applications?
我想控制这个应用程序配置文件中,如在生产中的应用使用托管SQL数据库。
I want to control this in the application configuration file, as in production the application uses a hosted sql database.
推荐答案
谢谢大家,我用你的反应的组合。
Thanks everyone, I used a combination of your responses.
在我的app.config文件我的连接字符串被定义如下:
In my app.config file my connection string is defined as follows
<add name="MyConnectionString"
connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Database=MyDatabaseForTesting;Trusted_Connection=Yes;" />
在我的单元测试类我用设置DataDirectory属性以下
In my unit test class I set the DataDirectory property using the following
[TestInitialize]
public void TestInitialize()
{
AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Databases"));
// rest of initialize implementation ...
}
这篇关于SQL防爆preSS连接字符串:相对于应用程序位置的MDF文件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!