带有数据库文件相对路径的连接字符串
我从 winforms 应用程序中的 sdf 数据库加载数据。我使用数据库文件的完整路径。示例:
conn = new SqlCeConnection
{
ConnectionString ="Data Source=F:\\My Documents\\Project1\\bin\\Debug\\Database.sdf"
};
我想使用数据库文件的相对路径。例如。我在文件夹 F:\My Documents\Project1\bin\Debug\Data\file.sdf 中有 sdf 文件,我想在连接字符串中使用相对路径。 有什么建议吗?谢谢。
I load data from sdf database in winforms App. I use full path to the database file . Example :
conn = new SqlCeConnection
{
ConnectionString ="Data Source=F:\\My Documents\\Project1\\bin\\Debug\\Database.sdf"
};
I d like use a relative path to the database file. For example. I have sdf file in folder F:\My Documents\Project1\bin\Debug\Data\file.sdf and I want use relative path in connection string.
Any advice ? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
相对路径:
将DataDirectory修改为可执行文件的路径:
Relative path:
Modifying DataDirectory as executable's path:
如果数据库文件存在,请尝试将此代码添加到工作目录,如下所示。
D:\HMProject\DataBase\HMProject.sdf
.sdf 文件的连接字符串
谢谢
ck.Nitin (TinTin)
Try this code to the working directory if database file exists like below.
D:\HMProject\DataBase\HMProject.sdf
Connection string for .sdf file
<add name="LocalDB" connectionString="metadata=res://*/Client.HMProject.csdl|res://*/Client.HMProject.ssdl|res://*/Client.HMProject.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\Database\HMProjectDB.sdf;Password=HMProject;Persist Security Info=False;"" providerName="System.Data.EntityClient" />
Thanks
ck.Nitin (TinTin)
在连接字符串中的相对路径出现几个奇怪的错误之后,我觉得有必要将其发布在这里。
使用“|DataDirectory|”时或“~”,则不允许使用“../”前进和退出!
示例是使用多个项目访问放置在其中一个项目中的相同 localdb 文件。
“~/../other”和“|DataDirectory|/../other”将会失败
即使写得很清楚在 MSDN 这里 它给出的错误有点不清楚,很难找到,而且在 SO 找不到它。
After several strange errors with relative paths in connectionstring I felt the need to post this here.
When using "|DataDirectory|" or "~" you are not allowed to step up and out using "../" !
Example is using several projects accessing the same localdb file placed in one of the projects.
" ~/../other" and " |DataDirectory|/../other" will fail
Even if it is clearly written at MSDN here the errors it gave where a bit unclear so hard to find and could not find it here at SO.
相对于什么,您的应用程序?如果是这样,那么您可以简单地使用以下命令获取应用程序的当前路径:
并将其附加到连接字符串中
Relative to what, your application ? If so then you can simply get the applications current Path with :
And append it to the connection string
在您的配置文件中给出相对路径
将 DataDirectory 更改为可执行路径
如果您使用的是 EntityFramework,那么您可以在 Context 类中设置 DataDirectory 路径
In your config file give the relative path
Change the DataDirectory to your executable path
If you are using EntityFramework, then you can set the DataDirectory path in your Context class
请尝试使用下面的代码块,这正是您正在寻找的:
Would you please try with below code block, which is exactly what you're looking for:
我在 web.config 文件中做到了这一点。我添加了 Sobhan 的答案,谢谢顺便说一句。
其中“db”成为我的数据库目录,而不是“App_Data”目录。
并正常打开:
var db = Database.Open("listdb");
I did this in the web.config file. I added to Sobhan's answer, thanks btw.
Where "db" becomes my database directory instead of "App_Data" directory.
And opened normally with:
var db = Database.Open("listdb");
我在尝试指定连接到 Windows 窗体应用程序的数据库的相对文件路径时遇到了同样的问题。我能够按照 Microsoft 向 Windows 窗体添加数据源的说明解决该问题(例如 用于连接 Access 数据库)。
通过使用此方法,Visual Studio 将为您设置数据库的相对文件路径,而不是尝试手动设置。如果您的数据库位于应用程序外部,它将创建数据库的副本并将其添加到应用程序的正确位置。尽管您可以在 App.config 和/或 Settings.settings 或某个脚本中手动更改连接字符串,但我发现这种方法很容易出错。相反,我发现一般情况下最好遵循 Microsoft 的说明。
I had the same issue trying to specify the relative file path for a database connected to a Windows Forms application. I was able to resolve the issue by following the directions for adding a data source to Windows Forms from Microsoft (e.g., for connecting an Access database).
By using this method, Visual Studio will set the relative file paths to your database for you instead of trying to set it manually. If your database is external to your application, it will create a copy of the database and add it to your application in the proper location. Although you can manually alter you connection string in App.config and/or Settings.settings or within one of your scripts, I've found this method to be error prone. Instead, I've found it best to follow the Microsoft instructions, in general.
这对我有用:
我正在查询 XLSX 文件,因此不必担心连接字符串中除数据源之外的任何其他内容。
所以我的答案是:
This worked for me:
I'm querying an XLSX file so don't worry about any of the other stuff in the connection string but the Data Source.
So my answer is: