Mdf-файл строки подключения в папке верхнего уровня - PullRequest
0 голосов
/ 09 февраля 2012

У меня есть следующая строка подключения:

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

Как я знаю, |DataDirectory| - это папка /bin/debug.Файл mdf находится в папке Datenbank, то есть точно в папке, которую я набрал в строке подключения.

Кажется, что и ..\\ не сработает.У кого-нибудь есть решение этой проблемы?

1 Ответ

2 голосов
/ 09 февраля 2012

Вы можете просто рассчитать dir, используя следующий код.

//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);
...