2009-12-02 56 views
30

我从winforms应用程序中的sdf数据库加载数据。我使用数据库文件的完整路径。例如:连接字符串与数据库文件的相对路径

conn = new SqlCeConnection 

{ 

ConnectionString ="Data Source=F:\\My Documents\\Project1\\bin\\Debug\\Database.sdf" 

}; 

我喜欢使用数据库文件的相对路径。例如。我在文件夹F中有sdf文件:\ My Documents \ Project1 \ bin \ Debug \ Data \ file.sdf,我想在连接字符串中使用相对路径。 有什么建议吗?谢谢。

+0

你确定你有相对路径吗?你输入的“F:\ My Documents \ Project1 \ bin \ Debug \ Data \ file.sdf”也是一个完整路径。如果您的应用程序已经从“F:\ My Documents \ Project1 \ bin \”运行,则相对路径将类似“\ data \ file.sdf” – Jrud 2009-12-02 15:35:10

回答

4

相对于你的应用程序?如果是这样,那么你可以简单地得到应用电流路径以:

System.Environment.CurrentDirectory 

,并追加到连接字符串

+4

您不能只将它“附加到连接字符串”。您必须解析连接字符串,提取数据源值,在当前目录前加上,然后重新生成连接字符串。这是不平凡的。 – 2011-01-05 18:41:40

75

相对路径:

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

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

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

应标记为答案。管理员可以做些什么吗? – 2013-09-18 13:27:42

+1

要了解有关** DataDirectory **术语的更多信息,请查看http://msdn.microsoft.com/en-us/library/cc716756.aspx(它位于文档末尾) – 2014-03-14 15:34:59

0

请您尝试使用下面的代码块,这正是您所期待的纳克:

SqlConnection conn = new SqlConnection 
{ 
    ConnectionString = "Data Source=" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Database.sdf" 
}; 
0

这为我工作:

string Connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+HttpContext.Current.Server.MapPath("\\myPath\\myFile.db")+";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";

我查询一个XLSX文件,所以不要在连接字符串,但数据源在担心任何的其他的东西。

所以HttpContext.Current.Server.MapPath("\\myPath\\myFile.db")就是答案。

4

如果数据库文件像下面那样存在,请将此代码尝试到工作目录。

d:\ HMProject \数据库\ HMProject.sdf

string Path = Environment.CurrentDirectory; 
string[] appPath = Path.Split(new string[] { "bin" }, StringSplitOptions.None); 
AppDomain.CurrentDomain.SetData("DataDirectory", appPath[0]); 

连接字符串.sdf文件

<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=&quot;Data Source=|DataDirectory|\Database\HMProjectDB.sdf;Password=HMProject;Persist Security Info=False;&quot;" providerName="System.Data.EntityClient" />

由于

ck.Nitin(丁丁)

2

连接字符串中的相对路径发生几个奇怪的错误后,我觉得有必要在这里发布这个。

使用“| DataDirectory |”时或“〜”,你不可以使用“../”加强和缩小!

示例使用多个项目访问放置在其中一个项目中的相同localdb文件。

“〜/../其他”和“| DataDirectory目录| /../其他”会失败

即使是写清楚at MSDN here它给了其中一个有点不清楚很难找到错误在SO这里找不到它。

1
<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
    <!--FailIfMissing=false --> 
    <add key="DbSQLite" value="data source=|DataDirectory|DB.db3;Pooling=true;FailIfMissing=false"/> 
    </appSettings> 
</configuration> 
+3

不存储连接字符串在。他们有自己的部分。 – slfan 2015-08-02 07:45:23

0

在您的配置文件中给出的相对路径

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

更改DataDirectory目录到你的可执行文件路径

string path = AppDomain.CurrentDomain.BaseDirectory; 
AppDomain.CurrentDomain.SetData("DataDirectory", path); 

如果您正在使用的EntityFramework,那么你可以设置在DataDirectory目录路径你的上下文类

0

我在web.config文件中做了这个。我添加到Sobhan的答案,谢谢btw。

<connectionStrings> 
    <add name="listdb" connectionString="Data Source=|DataDirectory|\db\listdb.sdf"/> 
    </connectionStrings> 

其中“db”成为我的数据库目录而不是“App_Data”目录。

并与常开:

变种分贝= Database.Open( “listdb”);

相关问题