【问题标题】:Connectionstring mdf file in superior folder上级文件夹中的连接字符串 mdf 文件
【发布时间】:2012-02-09 19:34:41
【问题描述】:

我有以下连接字符串:

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

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

看来,..\\ 不起作用。 有人有解决这个问题的办法吗?

【问题讨论】:

  • 您看到的确切错误是什么?
  • 我相信 |DataDirectory|指项目文件夹下的“App_Data”文件夹。
  • 请不要在您的标题前加上“C#”之类的前缀。这就是标签的用途。

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


【解决方案1】:

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

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

【讨论】:

  • 您使用的是什么版本的 csharp?显然,您需要包含 System.IOSystem.Reflection 命名空间才能使其正常工作。我怀疑这是问题所在,只是把它放在那里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多