使用 System.Data.SQLite 如何使用相对路径在连接字符串中指定数据库文件

发布于 2024-07-09 12:10:49 字数 75 浏览 7 评论 0原文

想要将我的项目部署在不同的服务器上,我希望能够使用相对路径指定连接字符串。 我似乎无法让它发挥作用,想知道是否有一些技巧......?

Wanting to deploy my project on different servers I would prefer to be able to specify a connect string using a relative path. I can't seem to get that to work and want to know if there is some trick to it...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

烟燃烟灭 2024-07-16 12:10:49

建议

您可以在应用程序中构建绝对路径并将其传递到连接字符串中。

因此,如果您知道数据库文件位于应用程序文件夹的 database 子文件夹中,您可以执行以下操作 (C#):(

    string relativePath = @"database\myfile.s3db";
    string currentPath;
    string absolutePath;
    string connectionString;

    currentPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
    absolutePath = System.IO.Path.Combine(currentPath,relativePath);

    connectionString = string.Format("DataSource={0}", absolutePath);

    SQLiteConnection cnn = new SQLiteConnection(connectionString);

有人可能可以纠正我如何获取当前路径) 。

A suggestion

You could build the absolute path in the app and pass that in the connection string.

So, if you know that the database file is in the database subfolder of the application folder, you could do something like this (C#):

    string relativePath = @"database\myfile.s3db";
    string currentPath;
    string absolutePath;
    string connectionString;

    currentPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
    absolutePath = System.IO.Path.Combine(currentPath,relativePath);

    connectionString = string.Format("DataSource={0}", absolutePath);

    SQLiteConnection cnn = new SQLiteConnection(connectionString);

(Someone can probably correct me on how to get the current path).

素衣风尘叹 2024-07-16 12:10:49

这个怎么样?

"Data Source=|DataDirectory|mydb.db;..."

我相信 |DataDirectory| 指向您的应用程序所在的目录。 我使用 NHibernate,它适用于以下内容:

<add key="hibernate.connection.connection_string"
       value="Data Source=|DataDirectory|mydb.db;Version=3;Compress=False;synchronous=OFF;" >

How about this?

"Data Source=|DataDirectory|mydb.db;..."

I believe |DataDirectory| point to the directory where your app is located. I use NHibernate and it works with the following:

<add key="hibernate.connection.connection_string"
       value="Data Source=|DataDirectory|mydb.db;Version=3;Compress=False;synchronous=OFF;" >
爱,才寂寞 2024-07-16 12:10:49

像这样:

String currentPath = System.IO.Path.GetDirectoryName( Application.ExecutablePath );

Like this:

String currentPath = System.IO.Path.GetDirectoryName( Application.ExecutablePath );

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