程序远离数据库文件时如何运行?

时间:2020-02-14 10:03:09

标签: c# sqlite

我正在尝试运行我的程序,该程序远离sqlite数据库文件,但是出现了一个异常提示

Data Source cannot be empty.  Use :memory: to open an in-memory database

这是我的代码

    static string path;
    static SQLiteConnection cnn = new SQLiteConnection(@"Data Source=" + path + ";Pooling=true;FailIfMissing=false;Version=3");

    private void Form_Load(object sender, EventArgs e) {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == DialogResult.OK)
            path = ofd.FileName;
        OpenDB();
    }

2 个答案:

答案 0 :(得分:0)

在分配了path值后,请尝试创建连接对象。

SQLiteConnection cnn; // requires not to be accessed until assigned
// hosting connection in the form is not the best idea so you may try to create data access object (additional class) with DB access implemented in this new class 

void OpenDB(string filename)
{
 // you may want to add existing connection check and disposing code here 
 cnn = new SQLiteConnection(@"Data Source=" + filename + ";Pooling=true;FailIfMissing=false;Version=3");
}

应该像

一样使用
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            path = ofd.FileName;
            OpenDB(path);
        }

答案 1 :(得分:0)

我必须从项目的文件夹中创建一个快捷方式。

相关问题