SQLiteConnection DisposedObject

时间:2012-10-18 09:32:00

标签: c# sqlite exception

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一次,因此析构函数中的connections = 1,但我不明白为什么它会在类析构函数完成之前处理对象。

1 个答案:

答案 0 :(得分:6)

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

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

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

关于数据库连接,最好的方法是尽可能早地打开并尽快关闭。