public void UsingSafeConnection(string sqlStr, Action <SQLiteDataReader> callBack) { var sql = new SQLiteString(sqlStr); using (SQLiteConnection con = new SQLiteConnection(DataSource)) { con.Open(); using (SQLiteCommand com = new SQLiteCommand(con)) { try { com.CommandText = sql.SqlText; sql.SetCommand(com); var reader = com.ExecuteReader(); callBack(reader); reader.Close(); } catch (Exception ex) { throw new Exception(string.Format("SQLite database [{0}] occurse errors", DataSource), ex); } finally { con.Close(); con.Dispose(); } } } }
public void Find(string sql, Action <dynamic> callback) { try { var s = new SQLiteString(sql); using (SQLiteConnection con = new SQLiteConnection(DataSource)) { con.Open(); SQLiteCommand com = new SQLiteCommand(con); s.SetCommand(com); using (var reader = com.ExecuteReader()) { while (reader.Read()) { callback(reader.ToDynamic()); } reader.Close(); } con.Close(); } } catch { } }
public void UsingSafeConnection(string sqlStr, Action <DBDataReader> callBack) { var sql = new SQLiteString(sqlStr); using (SQLiteConnection con = new SQLiteConnection(DataSource)) { con.Open(); using (SQLiteCommand com = new SQLiteCommand(con)) { try { com.CommandText = sql.SqlText; sql.SetCommand(com); var reader = com.ExecuteReader(); callBack(new DBDataReader(reader)); reader.Close(); } catch (Exception ex) { Log4NetService.LoggerManagerSingle.Instance.Error(ex, string.Format("SQLite database [{0}] occurse errors", DataSource)); } finally { con.Close(); con.Dispose(); } } } }
/// <summary> /// 执行sql,返回首行首列的值 /// </summary> public string ExecuteScalar(SQLiteString sql) { string result = string.Empty; UsingSafeConnection(com => { com.CommandText = sql.SqlText; sql.SetCommand(com); result = com.ExecuteScalar().ToSafeString(); }); return(result); }
/// <summary> /// 执行Sql,返回受影响的行数 /// </summary> public int ExecuteNonQuery(SQLiteString sql) { int result = 0; UsingSafeConnection(com => { com.CommandText = sql.SqlText; sql.SetCommand(com); result = com.ExecuteNonQuery(); }); return(result); }
public void UsingExistingConnection(SQLiteString sql, Action <SQLiteDataReader> callBack) { using (SQLiteCommand com = new SQLiteCommand(ExistingConnection)) { try { com.CommandText = sql.SqlText; sql.SetCommand(com); var reader = com.ExecuteReader(); callBack(reader); } catch (Exception ex) { throw new Exception(string.Format("SQLite database [{0}] occurse errors", DataSource), ex); } } }