关闭并重新打开 sqlite 连接后,插入或更新查询不起作用 - WP8 C#
After an sqlite connection is closed and reopened, insert or update queries dont work - WP8 C#
我有一个 DBManager class,它实现了 IDisposable 接口。 class 有一个 sql_conn
变量。它具有创建新连接的 OpenConnection 函数。 class还有建表、更新记录等功能
public DBManager()
{
sql_conn = new SQLiteConnection("MyDB.db");
}
public void Dispose()
{
sql_conn.Dispose();
}
每次我需要更新一些值时,我都会创建一个DBMan对象并打开连接
using (DBManager dbMan = new DBManager())
{
dbMan.OpenConnection();
dbMan.InsertIntoDB(val1, val2);
}
由于 DBManager 已经实现了 IDisposable,它会在 using 语句完成后释放 sql_conn
。
我现在面临的问题是,在其中一个 classes 中,我需要根据某些检查更新同一行的多个值。
void SaveValues
{
save1();
save2();
save3();
}
在save1中,我打开连接,更新记录,关闭连接
在save2中,我打开连接并更新记录,然后关闭。
public void save1()
{
using (DBManager dbMan = new DBManager())
{
dbMan.OpenConnection();
if(//check)
{
dbMan.InsertIntoDB(val1, val2);// update query is used
}
}
}
public void save2()
{
using (DBManager dbMan = new DBManager())
{
dbMan.OpenConnection();
if(//check)
{
dbMan.InsertIntoDB(val3, val4);
}
}
}
Save1 有效,值在数据库中更新。但是 save2 不是。该函数不会抛出错误,但不会更新数据库中的值。
知道为什么这不起作用吗?
在 SQLite.Net 中执行 Dispose SQLiteConnection
的代码如下所示:
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
Close ();
}
public void Close ()
{
if (_open && Handle != NullHandle) {
try {
if (_mappings != null) {
foreach (var sqlInsertCommand in _mappings.Values) {
sqlInsertCommand.Dispose();
}
}
var r = SQLite3.Close (Handle);
if (r != SQLite3.Result.OK) {
string msg = SQLite3.GetErrmsg (Handle);
throw SQLiteException.New (r, msg);
}
}
finally {
Handle = NullHandle;
_open = false;
}
}
}
来自 MSDN,SuppressFinalize 执行以下操作:
Requests that the common language runtime not call the finalizer for the specified object.
所以,我不确定这是否是对象似乎仍处于打开状态的原因,因此使您自己的 Dispose 函数强制 GC 收集 GC.Collect();
应该 完成或关闭对象。
来自相似的question
What happens when you call SQLiteConnection.Close() is that (along with a number of checks and other things) the SQLiteConnectionHandle that points to the SQLite database instance is disposed. This is done through a call to SQLiteConnectionHandle.Dispose(), however this doesn't actually release the pointer until the CLR's Garbage Collector performs some garbage collection. Since SQLiteConnectionHandle overrides the CriticalHandle.ReleaseHandle() function to call sqlite3_close_interop() (through another function) this does not close the database.
希望对您有所帮助。
我有一个 DBManager class,它实现了 IDisposable 接口。 class 有一个 sql_conn
变量。它具有创建新连接的 OpenConnection 函数。 class还有建表、更新记录等功能
public DBManager()
{
sql_conn = new SQLiteConnection("MyDB.db");
}
public void Dispose()
{
sql_conn.Dispose();
}
每次我需要更新一些值时,我都会创建一个DBMan对象并打开连接
using (DBManager dbMan = new DBManager())
{
dbMan.OpenConnection();
dbMan.InsertIntoDB(val1, val2);
}
由于 DBManager 已经实现了 IDisposable,它会在 using 语句完成后释放 sql_conn
。
我现在面临的问题是,在其中一个 classes 中,我需要根据某些检查更新同一行的多个值。
void SaveValues
{
save1();
save2();
save3();
}
在save1中,我打开连接,更新记录,关闭连接 在save2中,我打开连接并更新记录,然后关闭。
public void save1()
{
using (DBManager dbMan = new DBManager())
{
dbMan.OpenConnection();
if(//check)
{
dbMan.InsertIntoDB(val1, val2);// update query is used
}
}
}
public void save2()
{
using (DBManager dbMan = new DBManager())
{
dbMan.OpenConnection();
if(//check)
{
dbMan.InsertIntoDB(val3, val4);
}
}
}
Save1 有效,值在数据库中更新。但是 save2 不是。该函数不会抛出错误,但不会更新数据库中的值。
知道为什么这不起作用吗?
在 SQLite.Net 中执行 Dispose SQLiteConnection
的代码如下所示:
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
Close ();
}
public void Close ()
{
if (_open && Handle != NullHandle) {
try {
if (_mappings != null) {
foreach (var sqlInsertCommand in _mappings.Values) {
sqlInsertCommand.Dispose();
}
}
var r = SQLite3.Close (Handle);
if (r != SQLite3.Result.OK) {
string msg = SQLite3.GetErrmsg (Handle);
throw SQLiteException.New (r, msg);
}
}
finally {
Handle = NullHandle;
_open = false;
}
}
}
来自 MSDN,SuppressFinalize 执行以下操作:
Requests that the common language runtime not call the finalizer for the specified object.
所以,我不确定这是否是对象似乎仍处于打开状态的原因,因此使您自己的 Dispose 函数强制 GC 收集 GC.Collect();
应该 完成或关闭对象。
来自相似的question
What happens when you call SQLiteConnection.Close() is that (along with a number of checks and other things) the SQLiteConnectionHandle that points to the SQLite database instance is disposed. This is done through a call to SQLiteConnectionHandle.Dispose(), however this doesn't actually release the pointer until the CLR's Garbage Collector performs some garbage collection. Since SQLiteConnectionHandle overrides the CriticalHandle.ReleaseHandle() function to call sqlite3_close_interop() (through another function) this does not close the database.
希望对您有所帮助。