[英]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:
变量将被扩展如下:
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.