class SQLiteDatabase
    {
        String dbConnection;
        static SQLiteConnection cnn;
        static int connections = 0;
        /// <summary>
        ///     Default Constructor for SQLiteDatabase Class.
        /// </summary>
        public SQLiteDatabase()
        {
            dbConnection = "Data Source=SQLiteOphthaMetrics.db;foreign keys=true;";
            if (connections == 0)
            {
                cnn = new SQLiteConnection(dbConnection);
                cnn.Open();
                this.ExecuteNonQuery("CREATE TABLE IF NOT EXISTS patients ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, birthday TEXT, gender TEXT, iriscolor INTEGER);");
                this.ExecuteNonQuery("CREATE TABLE IF NOT EXISTS images ( nameRed TEXT NOT NULL PRIMARY KEY, checksumRed TEXT NOT NULL, "
                    + "nameGreen TEXT NOT NULL, checksumGreen TEXT NOT NULL, state INTEGER, cvalue REAL, sharpness REAL, deviation REAL, area REAL, redExposure REAL, "
                    + "redGain REAL, greenExposure REAL, greenGain REAL, zfocus INTEGER, flipped INTEGER, patientID INTEGER, FOREIGN KEY (patientID) REFERENCES patients(id));");
            }
            connections++;
        }

        ~SQLiteDatabase()
        {
            connections--;
            if (connections == 0)
            {
                cnn.Close();
                cnn.Dispose();
            }
        }
    }

此代码抛出 DisposedObjectException
System.ObjectDisposedException was unhandled
  Message=Auf das verworfene Objekt kann nicht zugegriffen werden.
Objektname: "SQLiteConnection".
  Source=System.Data.SQLite
  ObjectName=SQLiteConnection
  StackTrace:
       bei System.Data.SQLite.SQLiteConnection.CheckDisposed()
       bei System.Data.SQLite.SQLiteConnection.Close()
       bei EyeScanner.SQLiteDatabase.Finalize()
  InnerException:

我目前只在代码中调用 SQLiteDatabase 一次,因此在析构函数中连接 = 1,但我不明白为什么它会在类析构函数完成之前处理对象。

最佳答案

我相信您正在尝试在类实例准备好处理后处理连接。如果您在 SQLiteConnection 中使用 using 语句会更好。 SQLiteConnection 类实现了 IDisposable,您将能够执行以下操作:

using(cnn = new SQLiteConnection(dbConnection))
{
     cnn.Open();
     this.ExecuteNonQuery("...your query");
}

这将作为一个 try/finally 块,即使发生异常,连接也会在 using 块之后关闭并处理。

关于数据库连接,最好的办法是越晚打开越早关闭。

关于c# - SQLiteConnection DisposedObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12951457/

10-10 22:10