C# ADO.NET |数据目录| 这是在哪里记录的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1409358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ADO.NET |DataDirectory| where is this documented?
提问by programmernovice
In AppConfig it is possible to use |DataDirectory|
but I can't find any doc ?
在 AppConfig 中可以使用 |DataDirectory|
但我找不到任何文档?
采纳答案by Alex
|DataDirectory|
is a substitution string so you can configure the location of your database file separately.
|DataDirectory|
是一个替换字符串,因此您可以单独配置数据库文件的位置。
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");
回答by codekaizen
There is an internal class called SqlConnectionHelper which parses this and creates the MDF if needed.
有一个名为 SqlConnectionHelper 的内部类,它会解析它并在需要时创建 MDF。
Here's the only MS doc I can find about that class and the |DataDirectory| macro: http://msdn.microsoft.com/en-us/library/aa478948.aspx.
这是我能找到的关于该类和 |DataDirectory| 的唯一 MS 文档。宏:http: //msdn.microsoft.com/en-us/library/aa478948.aspx。
回答by MRG
In the MSDN social forums thisanswer can be found
在 MSDN 社交论坛中可以找到这个答案
|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.
|数据目录| (包含在管道符号中)是一个替换字符串,指示数据库的路径。它消除了硬编码完整路径的需要,这会导致几个问题,因为数据库的完整路径可以在不同的地方序列化。DataDirectory 还使共享项目和部署应用程序变得容易。
For example, instead of having the following connection string:
例如,不要使用以下连接字符串:
"Data Source= c:\program files\MyApp\Mydb.sdf"
Using DataDirectory, you can have the following connection string:
使用 DataDirectory,您可以拥有以下连接字符串:
“Data Source = |DataDirectory|\Mydb.sdf”
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
要设置 DataDirectory 属性,请调用 AppDomain.SetData 方法。如果未设置 DataDirectory 属性,将应用以下默认规则来访问数据库文件夹:
- For applications that are put in a folder on the user's computer, the database folder uses the application folder.
- For applications that are running under ClickOnce, the database folder uses the specific data folder that is created.
- 对于放置在用户计算机上的文件夹中的应用程序,数据库文件夹使用应用程序文件夹。
- 对于在 ClickOnce 下运行的应用程序,数据库文件夹使用创建的特定数据文件夹。
回答by Joe.P
http://msdn.microsoft.com/en-us/library/aa478948.aspx
http://msdn.microsoft.com/en-us/library/aa478948.aspx
The |DataDirectory| portion of the connection string specifies that the MDF file is located in the App_Data directory.
|数据目录| 连接字符串的一部分指定 MDF 文件位于 App_Data 目录中。
回答by Paul Ford
Incorrect guys! The |DataDirectory| refers to the mssql\data directory your instance is configured for.
不正确的家伙!|数据目录| 指的是为您的实例配置的 mssql\data 目录。
So for example I am using Visual Studio 2012 inconjunction with SQL Express. |DataDirectory| places all MDF files under C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA where my sql express was installed not my solutions app_data folder.
例如,我将 Visual Studio 2012 与 SQL Express 结合使用。|数据目录| 将所有 MDF 文件放在 C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA 下,我的 sql express 安装在我的解决方案 app_data 文件夹中。
Also the file is names MVCMovie.Models.MovieDBContext not Movies.mdf as specified in my web.config.
此外,该文件的名称为 MVCMovie.Models.MovieDBContext 而不是我的 web.config 中指定的 Movies.mdf。
I'm thinking it needs to be configured somewhere in visual studio for it to be placed appropriately under app_data.
我认为它需要在visual studio中的某个地方进行配置,以便将它适当地放置在app_data下。
回答by tggm
This might be relevant if you're using code first migrations.
如果您使用代码优先迁移,这可能是相关的。
With VisualStudio 2013 (at least) when running an Update-Database command the data directory is relative from the "Startup Project" currently configured in visual studio.
对于 VisualStudio 2013(至少),在运行 Update-Database 命令时,数据目录与当前在 Visual Studio 中配置的“启动项目”相关。
Even if you run the Update-Database on another project (as selected on the Package Manager Console), it will create your database on the App_Data of the currently selected Startup Project.
即使您在另一个项目上运行 Update-Database(在包管理器控制台上选择),它也会在当前选择的启动项目的 App_Data 上创建您的数据库。