Connectionstring mdf文件位于上级文件夹中

时间:2012-02-09 19:34:42

标签: c# connection-string sql-server-2008-express mdf

我有以下连接字符串:

string connectionstr = "Data Source=.\\SQLEXPRESS;" + "AttachDbFilename=|DataDirectory|\\..\\..\\Datenbank\\FarmersCalc.mdf;" + "Integrated Security=True;" + "User Instance=true;";

据我所知,|DataDirectory|/bin/debug - 文件夹。 mdf文件位于文件夹Datenbank中,这肯定位于我在连接字符串中输入的文件夹中。

似乎,..\\无效。 有没有人有解决这个问题的方法?

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码计算目录。

//these two lines get the executable's directory
Uri u = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase);
DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(u.LocalPath));

//this goes up two directories and combines that directory with the rest
//of the path to the file
string path = Path.Combine(d.Parent.Parent.FullName, @"Datenbank\FarmersCalc.mdf;");
Console.WriteLine(path);
相关问题