简体   繁体   中英

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; but when I specify the full path, the data is added to the database.

Can someone tell why using |DataDirectory| does not work?

The .NET runtime added support for the DataDirectory macro. This allows Visual Studio to put a special variable in the connection string that will be expanded at run-time.

So you can modify a connection string like this:

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

By default, the |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.
  • For apps running under ClickOnce, this will be a special data folder created by ClickOnce
  • For Web apps, this will be the App_Data folder

Under the hood, the value for |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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM