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,但我不明白为什么它会在类析构函数完成之前处理对象。