简体   繁体   English

获取sqlite数据库文件路径

[英]Get path of sqlite database file

Hi everyone i can't set a relative path to open my database, the project is structured in this mood大家好,我无法设置打开数据库的相对路径,该项目就是在这种情况下构建的

projectname
db/acces.db
info/try.cs

This is the code inside try.cs with which i should open the database, obviously if I put the full path it goes:这是 try.cs 中的代码,我应该用它来打开数据库,显然,如果我输入完整路径,它就会:

 try {
    using (SQLiteConnection conn = new SQLiteConnection(@"data source="db/access.db")
      {
        conn.Open();
        using (SQLiteCommand fmd = conn.CreateCommand()){
        fmd.CommandText = @"SELECT * FROM 'test';
        fmd.CommandType = CommandType.Text;
        SQLiteDataReader r = fmd.ExecuteReader();
        while (r.Read())
          Debug.Writeline("foo");
        conn.Close();
      }
    }
 catch (Exception e){
    Debug.WriteLine("Errors");
  }

I should take the path of the acces.db file, not complete but relative to my project, to be set in data source, how should i do?我应该把acces.db文件的路径,不完整但是相对于我的项目,在数据源中设置,我该怎么办?

You can use something like this:你可以使用这样的东西:

    using System.IO;
var connection = "Data Source=" + Path.Combine(Directory.GetCurrentDirectory(), "db\\access.db")
    try {
            using (SQLiteConnection conn = new SQLiteConnection(connection)
              {
                conn.Open();
                using (SQLiteCommand fmd = conn.CreateCommand()){
                fmd.CommandText = @"SELECT * FROM 'test';
                fmd.CommandType = CommandType.Text;
                SQLiteDataReader r = fmd.ExecuteReader();
                while (r.Read())
                  Debug.Writeline("foo");
                conn.Close();
              }
            }
         catch (Exception e){
            Debug.WriteLine("Errors");
          }

You said you should get something of type Data Source=C:\\Users\\mypc\\Documents\\tesrepo\\Myapp\\testApp\\db\\test.db .你说你应该得到Data Source=C:\\Users\\mypc\\Documents\\tesrepo\\Myapp\\testApp\\db\\test.db的东西。

Here is how to get the correct path.以下是如何获得正确的路径。

Project path:项目路径:

Directory.GetParent(workingDirectory).Parent.FullName;

Add the path you want at the end: For example, the address of the 123.mp3 file in the music folder here is:在最后添加你想要的路径:比如这里music文件夹中123.mp3文件的地址是:

在此处输入图像描述

string filepath = Path.Combine(projectDirectory + "\\music\\123.mp3");

//Get db address //获取数据库地址

string workingDirectory = Environment.CurrentDirectory;
string projectDirectory = Directory.GetParent(workingDirectory).Parent.FullName;
string dbpath = Path.Combine(projectDirectory + "\\db\\test.db");

dbpath is what you want. dbpath是你想要的。

If you have any questions about my code, please add a comment below.如果您对我的代码有任何疑问,请在下面添加评论。

Updated:更新:

You can import the updated file into the output directory through this operation.通过该操作可以将更新后的文件导入到output目录下。

在此处输入图像描述

Your path would work without any modification.您的路径无需任何修改即可工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM