连接字符串到数据库文件的相对路径 [英] Connection string with relative path to the database file

查看:226
本文介绍了连接字符串到数据库文件的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我WinForms应用程序加载从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喜欢使用相对路径到数据库文件。例如。我在文件夹˚FSDF文件:\\我的文档\\ PROJECT1 \\ BIN \\调试\\ DATA \\ file.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.

推荐答案

相对路径:

        ConnectionString = "Data Source=|DataDirectory|\Database.sdf";

修改DataDirectory目录作为可执行文件的路径:

Modifying DataDirectory as executable's path:

        string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
        string path = (System.IO.Path.GetDirectoryName(executable));
        AppDomain.CurrentDomain.SetData("DataDirectory", path);

这篇关于连接字符串到数据库文件的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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