为什么DataDirectory不会在运行时更改(Why doesn't DataDirectory change on run time)

编程入门 行业动态 更新时间:2024-10-11 15:16:35
为什么DataDirectory不会在运行时更改(Why doesn't DataDirectory change on run time)

当我将连接字符串设置为Data: Source=C:\Users\PACKARD BELL\documents\visual studio 2010\Projects\GestionStock\MyApp\Mydb.sdf时,我想用w winform c#application连接到本地数据库 它工作正常,但当我将它设置为Data Source=|DataDirectory|\Mydb.sdf它不工作我试图在控制台中打印连接字符串与数据目录变量,我发现它不会改变

我希望连接字符串随应用程序文件夹的位置而变化

我怎样才能做到这一点? 提前致谢

I am tring to connect to a local database with w winform c# application when i set my connection string to Data: Source=C:\Users\PACKARD BELL\documents\visual studio 2010\Projects\GestionStock\MyApp\Mydb.sdf it works fine but when i set it to Data Source=|DataDirectory|\Mydb.sdf it dosen't work i tried to print the connection string with the data directory variable in the console and i found that it dosen't change

i want the connection string to change with the location of the application folder

How can i do that? thanks in advance

最满意答案

根据MSDN :

|DataDirectory| :这是通过AppDomain.SetData("DataDirectory", objValue)方法设置的值。 ASP.NET应用程序解析| DataDirectory | 到“/ app_data”文件夹。

基本上,如果您使用|DataDirectory| 值,您的数据库必须存在于项目的App_Data\目录中。 您可以更改默认值,但实际上这只会使可能期望默认行为的其他开发人员感到困惑。

所以,而不是

Source=C:\Users\PACKARD BELL\documents\visual studio 2010/Projects\GestionStock\MyApp\Mydb.sdf ,

这将是

Source=C:\Users\PACKARD BELL\documents\visual studio 2010\Projects\GestionStock\MyApp\App_Data\Mydb.sdf

According to MSDN:

|DataDirectory|: This is the value that is set through the AppDomain.SetData("DataDirectory", objValue) method. An ASP.NET application resolves |DataDirectory| to the "/app_data" folder.

Essentially, if you use the |DataDirectory| value, your database must exist inside the App_Data\ directory in your project. You can change the default value, but in practice this only serves to confuse other developers who might be expecting the default behavior.

So, instead of

Source=C:\Users\PACKARD BELL\documents\visual studio 2010/Projects\GestionStock\MyApp\Mydb.sdf,

it would be

Source=C:\Users\PACKARD BELL\documents\visual studio 2010\Projects\GestionStock\MyApp\App_Data\Mydb.sdf

更多推荐

本文发布于:2023-08-04 02:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会在   DataDirectory   change   time   run

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!