为什么DataDirectory在运行时不更改 [英] Why doesn't DataDirectory change on run time

查看:120
本文介绍了为什么DataDirectory在运行时不更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用w winform c#应用程序连接到本地数据库 当我将连接字符串设置为数据时:Source=C:\Users\PACKARD BELL\documents\visual studio 2010\Projects\GestionStock\MyApp\Mydb.sdf
它工作正常,但当我将其设置为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

推荐答案

根据

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

|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.

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

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.

所以,而不是

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

这篇关于为什么DataDirectory在运行时不更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆