在我的 c# 应用程序中,我使用 sqlite 作为后端。当我执行简单的插入查询时,我花费的时间太长,最终出现数据库被锁定的消息。我在舞台服务器中部署了我的应用程序并远程执行操作。下面是我的代码,请帮忙。
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;
}
谢谢