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

时间:2022-09-25 21:45:36

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...?

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

3 个解决方案

#1


5  

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#):

因此,如果您知道数据库文件位于应用程序文件夹的数据库子文件夹中,则可以执行以下操作(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).

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

#2


5  

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:

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

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

#3


2  

Like this:

喜欢这个:

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

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

#1


5  

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#):

因此,如果您知道数据库文件位于应用程序文件夹的数据库子文件夹中,则可以执行以下操作(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).

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

#2


5  

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:

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

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

#3


2  

Like this:

喜欢这个:

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

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