关闭数据库并且无法从未打开的数据库错误创建命令

To close db and cannot create commands from unopened database error

    string dppath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");
    db = new SQLiteConnection(dppath);
    string DELETEPASSCODE_DETAIL = "DELETE FROM Table1;";
    db.Execute(DELETEPASSCODE_DETAIL);
    db.Close();

您好。当程序执行上面的代码并到达下面的代码时,我得到这个错误:cannot create commands from unopened database

     try
            {
                string dpPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");`
                if (dpPath.IndexOf("user.db3") < 0)
                {
                    CreateDB();
                    CreateTable();
                }
                else
                {
                    if (db == null)
                    {
                        db = new SQLiteConnection(dpPath);
                    }
                    var tableExist = db.GetTableInfo("Table1");
……

错误发生在这个db.GetTableInfo("Table1");

我第一次检查代码时犯了一些错误。现在,我重现了这个问题。

数据库不能为空,删除If语句。

变化:

 if (db == null)
            {
                db = new SQLiteConnection(_databasePath);
            }                
            var tableExist = db.GetTableInfo("Info");
            var table_Info = db.Table<Info>().ToList();

收件人:

    db = new SQLiteConnection(_databasePath);
            var tableExist = db.GetTableInfo("Info");
            var table_Info = db.Table<Info>().ToList();

db.Table<Info>().ToList() 也可用于获取 table 信息。