当我将连接字符串设置为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
更多推荐
发布评论