带有数据库文件相对路径的连接字符串

发布于 2024-08-13 01:35:54 字数 321 浏览 6 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

笑咖 2024-08-20 01:35:54

相对路径:

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);

Relative path:

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

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);
终陌 2024-08-20 01:35:54

如果数据库文件存在,请尝试将此代码添加到工作目录,如下所示。

D:\HMProject\DataBase\HMProject.sdf

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

.sdf 文件的连接字符串

谢谢

ck.Nitin (TinTin)

Try this code to the working directory if database file exists like below.

D:\HMProject\DataBase\HMProject.sdf

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

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)

护你周全 2024-08-20 01:35:54

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

使用“|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.

情释 2024-08-20 01:35:54

相对于什么,您的应用程序?如果是这样,那么您可以简单地使用以下命令获取应用程序的当前路径:

System.Environment.CurrentDirectory 

并将其附加到连接字符串中

Relative to what, your application ? If so then you can simply get the applications current Path with :

System.Environment.CurrentDirectory 

And append it to the connection string

凝望流年 2024-08-20 01:35:54

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

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

将 DataDirectory 更改为可执行路径

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

如果您使用的是 EntityFramework,那么您可以在 Context 类中设置 DataDirectory 路径

In your config file give the relative path

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

Change the DataDirectory to your executable path

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

If you are using EntityFramework, then you can set the DataDirectory path in your Context class

波浪屿的海角声 2024-08-20 01:35:54
   <?xml version="1.0"?>  
<configuration>  
  <appSettings>  
    <!--FailIfMissing=false -->  
    <add key="DbSQLite" value="data source=|DataDirectory|DB.db3;Pooling=true;FailIfMissing=false"/>  
  </appSettings>  
</configuration>  
   <?xml version="1.0"?>  
<configuration>  
  <appSettings>  
    <!--FailIfMissing=false -->  
    <add key="DbSQLite" value="data source=|DataDirectory|DB.db3;Pooling=true;FailIfMissing=false"/>  
  </appSettings>  
</configuration>  
家住魔仙堡 2024-08-20 01:35:54

请尝试使用下面的代码块,这正是您正在寻找的:

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

Would you please try with below code block, which is exactly what you're looking for:

SqlConnection conn = new SqlConnection
{
    ConnectionString = "Data Source=" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Database.sdf"
};
夜深人未静 2024-08-20 01:35:54

我在 web.config 文件中做到了这一点。我添加了 Sobhan 的答案,谢谢顺便说一句。

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

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

并正常打开:

var db = Database.Open("listdb");

I did this in the web.config file. I added to Sobhan's answer, thanks btw.

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

Where "db" becomes my database directory instead of "App_Data" directory.

And opened normally with:

var db = Database.Open("listdb");

仄言 2024-08-20 01:35:54

我在尝试指定连接到 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.

╰沐子 2024-08-20 01:35:54

这对我有用:

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")

This worked for me:

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\"";

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:

HttpContext.Current.Server.MapPath("\\myPath\\myFile.db")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文