简体   繁体   English

为什么使用 DataDirectory 不起作用但使用完整路径有效?

[英]Why does using DataDirectory not work but using a full path works?

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Source=|DataDirectory|\ITGS.accdb\ITGS.accdb");

I am using Source=|DataDirectory|\\ITGS.accdb but for some reason data is not added;我正在使用Source=|DataDirectory|\\ITGS.accdb但由于某种原因没有添加数据; but when I specify the full path, the data is added to the database.但是当我指定完整路径时,数据会添加到数据库中。

Can someone tell why using |DataDirectory|有人能说出为什么使用|DataDirectory| does not work?不起作用?

The .NET runtime added support for the DataDirectory macro. .NET 运行时添加了对 DataDirectory 宏的支持。 This allows Visual Studio to put a special variable in the connection string that will be expanded at run-time.这允许 Visual Studio 在连接字符串中放置一个特殊变量,该变量将在运行时扩展。

So you can modify a connection string like this:因此,您可以像这样修改连接字符串:

"Data Source=.\SQLExpress;AttachDbFileName=|DataDirectory|\data.mdf"

By default, the |DataDirectory|默认情况下,|DataDirectory| variable will be expanded as follow:变量将被扩展如下:

  • For applications placed in a directory on the user machine, this will be the app's (.exe) folder.对于放置在用户计算机上的目录中的应用程序,这将是应用程序的 (.exe) 文件夹。
  • For apps running under ClickOnce, this will be a special data folder created by ClickOnce对于在 ClickOnce 下运行的应用程序,这将是一个由 ClickOnce 创建的特殊数据文件夹
  • For Web apps, this will be the App_Data folder对于 Web 应用程序,这将是 App_Data 文件夹

Under the hood, the value for |DataDirectory|在幕后,|DataDirectory| 的值simply comes from a property on the app domain.仅来自应用程序域上的属性。 It is possible to change that value and override the default behavior by doing this:可以通过执行以下操作来更改该值并覆盖默认行为:

AppDomain.CurrentDomain.SetData("DataDirectory", newpath)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用| DataDirectory | 在多项目解决方案的连接字符串中 - using |DataDirectory| in connectionstring of multiproject solution 浏览SQL Server数据库的数据目录不起作用 - Browsing datadirectory for SQL Server database does not work 使用| DataDirectory |时,无法导入此数据库。 - This database cannot be imported when using |DataDirectory| 使用RenderedGeometry作为路径数据不起作用 - Using RenderedGeometry as Data of Path does not work 为什么这个请求可以使用 HttpWebRequest 而不是 RestSharp? - Why does this request works using HttpWebRequest but not with RestSharp? 请求 OAuth 令牌不能使用代码,但可以使用 Postman - Requesting OAuth token does not work using code but works using Postman 使用 |DataDirectory| 时 C# 本地数据库不更新 - C# local database not updating when using |DataDirectory| 为什么使用SQLITE where子句不起作用? - Why using SQLITE where clause does not work? 为什么HttpRequest不能在Windows上运行但在Mac上运行? - Why does HttpRequest not work on Windows but works on Mac? 为什么使用ConditionalAccessExpression会更改扩展方法的工作方式? - Why does using a ConditionalAccessExpression change how my extension method works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM