2012-02-09 66 views
0

从来就得到了以下的ConnectionString:ConnectionString的mdf文件

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

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

看来,因为..\\将无法​​正常工作。 有没有人有解决这个问题的办法?

+0

你看到的确切错误是什么? – 2012-02-09 19:48:32

+0

我相信| DataDirectory |指的是项目文件夹下的“App_Data”文件夹。 – 2012-02-09 19:53:06

+2

请不要用“C#”等标题来标题。这就是标签的用途。 – 2012-02-09 20:04:02

回答

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); 
+0

路径不包含GetDirectoryName的定义... – user896692 2012-02-09 20:06:14

+0

您使用的是什么版本的csharp?你需要包含'System.IO'和'System.Reflection'命名空间才能工作,显然。我怀疑这是问题,只是把它放在那里。 – 2012-02-09 20:36:21