0
I N我的C#應用程序,我使用SQLite作爲後端。當我執行簡單的插入查詢時,我需要的時間太長,最終數據庫被鎖定的消息。我在舞臺服務器上部署了我的應用程序並遠程執行操作。下面是我的代碼,請幫助。SQLite數據庫鎖定錯誤使用C#應用程序
String sqlExpr = "INSERT INTO item (id, typeid, ownerid, created, modifiedby, modified,active,imageuploaded,logouploaded,language_item) VALUES (@Id,@type_TypeId ,@db_currentUser,@dates,@db_id ,@modified,@active_status,@image,@logo,@language)";
//using (SQLiteCommand _insertItem = new SQLiteCommand())
//{
SQLiteCommand _insertItem = new SQLiteCommand();
_insertItem.CommandText = sqlExpr;
_insertItem.Parameters.AddWithValue("@Id", Id);
_insertItem.Parameters.AddWithValue("@type_TypeId", type.TypeId);
_insertItem.Parameters.AddWithValue("@db_currentUser", db.currentUser.id);
_insertItem.Parameters.AddWithValue("@dates", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
_insertItem.Parameters.AddWithValue("@db_id", db.currentUser.id);
_insertItem.Parameters.AddWithValue("@modified", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
_insertItem.Parameters.AddWithValue("@active_status", active_status);
_insertItem.Parameters.AddWithValue("@image", "false");
_insertItem.Parameters.AddWithValue("@logo", "false");
_insertItem.Parameters.AddWithValue("@language", language);
rowsAffected = db.ExecuteNonQuerySQL(_insertItem);
db.Close();
public int ExecuteNonQuerySQL(SQLiteCommand cmd)
{
int ireturn = 0;
if (conn.State != ConnectionState.Open)//the connection is individual for each session so no need of lock .added the code on 2011-11-08 by Sangeetha
Open(DataFile);
//SQLiteConnection C = new SQLiteConnection("Data Source=database.db;Version=3;New=False;Compress=True;");
using (SQLiteTransaction dbtrans = conn.BeginTransaction(IsolationLevel.ReadCommitted))
{
using (SQLiteCommand cmd1 = (SQLiteCommand)cmd.Clone())
{
cmd.Dispose();
cmd1.Connection = conn;
cmd1.Transaction = dbtrans;
ireturn = cmd1.ExecuteNonQuery();
dbtrans.Commit();
cmd1.Dispose();
}
}
return ireturn;
}
謝謝
錯誤消息和錯誤代碼到底是什麼? –
數據庫文件被鎖定..這是我得到的錯誤.. – user642378
爲什麼將SQLcommand傳遞給方法並克隆它?爲什麼不每次都從頭開始創建它?保持連接打開,但不使用命令 – Phil