Ado.Net |Datadirectory| Where Is This Documented

ADO.NET |DataDirectory| where is this documented?

|DataDirectory| is a substitution string so you can configure the location of your database file separately.

So instead of:

SqlConnection c = new SqlConnection (
@"Data Source=.\SQLDB; AttachDbFilename=C:\MyDB\Database.mdf;Initial Catalog=Master");

you do the following:

// Set |DataDirectory| value
AppDomain.CurrentDomain.SetData("DataDirectory", "C:\myDB");

// SQL Connection String with |DataDirectory| substitution string
SqlConnection c = new SqlConnection (
@"Data Source=.\SQLDB; AttachDbFilename=|DataDirectory|\Database.mdf;Initial Catalog=Master");

How is location of |Data Directory| in connection strings resolved?

Here is the code which specifies where is |DataDirectory|

GetDataDirectory

[PermissionSet(SecurityAction.Assert, Unrestricted = true)]
internal static string GetDataDirectory() {
if (HostingEnvironment.IsHosted)
return Path.Combine(HttpRuntime.AppDomainAppPath, HttpRuntime.DataDirectoryName);

string dataDir = AppDomain.CurrentDomain.GetData(s_strDataDir) as string;
if (string.IsNullOrEmpty(dataDir)) {
string appPath = null;

#if !FEATURE_PAL // FEATURE_PAL does not support ProcessModule
Process p = Process.GetCurrentProcess();
ProcessModule pm = (p != null ? p.MainModule : null);
string exeName = (pm != null ? pm.FileName : null);

if (!string.IsNullOrEmpty(exeName))
appPath = Path.GetDirectoryName(exeName);
#endif // !FEATURE_PAL

if (string.IsNullOrEmpty(appPath))
appPath = Environment.CurrentDirectory;

dataDir = Path.Combine(appPath, HttpRuntime.DataDirectoryName);
AppDomain.CurrentDomain.SetData(s_strDataDir, dataDir, new FileIOPermission(FileIOPermissionAccess.PathDiscovery, dataDir));
}

return dataDir;
}

|DataDirectory| in Project properties Settings

This has been asked before. This MSDN post gives a good overview.

It should indeed default to your binaries folder, you can change it with AppDomain.SetData() . If you change it, better do it early.

Can't connect to localdb when using [DataDirectory] in Windows Forms C# .NET Core

finally found my solution , i was using System.Data.SqlClient which doesn't support |DataDirectory| in: AttachDbFilename= . the new SqlClient Provider package : Microsoft.Data.SqlClient is what should be used from now on which supports AppDomains and |DataDirectory| macro in AttachDbFilename=

for using the System.Data.SqlClient the DB location in the connection string should be retrieved using : AppDomain.CurrentDomain.BaseDirectory
so the connection string for System.Data.SqlClient Should be like this :

cstr = @"server=(localdb)\MSSQLLocalDB;AttachDbFilename="+ AppDomain.CurrentDomain.BaseDirectory+"Database.mdf ; Integrated Security = True";

this should fix the problem

Deploy problem with project with an access database integrated

If you are using the Publish functionality in VS then you are using ClickOnce. ClickOnce apps have a dedicated folder to which "|DataDirectory|" resolves at run time. It is NOT the program folder. If you want to use "|DataDirectory|" in your connection string then you have to specify that the data file is indeed a data file via the properties, so that it will be placed in that folder. I don't really use ClickOnce but I believe that, to do that, you need to open the Publish page of the project properties, click the Application Files button and then set the Publish Status of that file to Data File. I'm not sure whether the subfolders for the source file will be honoured after publishing or not but you can test that.

Note that, if you simply copy the application files to the target machine or use some installer technology other than ClickOnce then "|DataDirectory|" will resolve to the program folder. For the record, it resolves to the App_Data folder for ASP.NET apps (possibly only Web Forms, not sure).



Related Topics



Leave a reply



Submit