【问题标题】:Value cannot be null. Parameter name: path值不能为空。参数名称:路径
【发布时间】:2015-08-27 14:53:16
【问题描述】:

我正在使用 Windows 窗体应用程序中的本地数据库,并且我正在尝试将数据库的源实现为 DataDirectory,因此如果我将数据库从一台计算机移动到另一台计算机,它将正常工作美好的。我编写了以下代码,但在获取完整路径的行中出现此错误,即值不能为空。谢谢!

var dataDirectory = ConfigurationManager.AppSettings["DataDirectory"];
var absoluteDataDirectory = Path.GetFullPath(dataDirectory);
AppDomain.CurrentDomain.SetData("DataDirectory", absoluteDataDirectory);
var connString = (@"Data Source= |DataDirectory|\Angajati.sdf");

【问题讨论】:

  • 看一下错误。它已经准确地告诉你出了什么问题。
  • 为什么不从 app.config 中的连接字符串部分获取它?
  • 我知道错误说明了什么。但是不知道怎么解决。
  • 手动检查路径是否存在
  • 看起来不像。

标签: c# sql null localdb


【解决方案1】:

您的app.config 中应该有此部分:

<appSettings>
    <add key="DataDirectory" value="DataDirectoryPath"/>
</appSettings>

顺便说一句,访问 DataDirectory 你必须使用这个代码:

AppDomain.CurrentDomain.GetData("DataDirectory")

more info.

【讨论】:

  • 有效。表明路径是正确的,但它没有插入到数据库中。我是说。它一直到 /Debug,然后进入一个名为 DataDirectoryPath 的文件夹。为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多