0

I´ve got the following connectionstring:

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

As i know, |DataDirectory| is the /bin/debug- folder. The mdf file is in the folder Datenbank, that is for sure in the folder that I typed in in the connection string.

It seems, as ..\\ would not work. Does anybody have a solution for that problem?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user896692
  • 2,351
  • 7
  • 36
  • 57

1 Answers1

2

You could just calculate the dir using the following code.

//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);
Tim Coker
  • 6,484
  • 2
  • 31
  • 62
  • What version of csharp are you using? You need to include the `System.IO` and `System.Reflection` namespaces for this to work, obviously. I doubt that's the problem, just putting it out there. – Tim Coker Feb 09 '12 at 20:36