/// <summary> /// 执行sql,返回受影响条记录数 /// </summary> /// <param name="sql"></param> /// <param name="pl"></param> /// <param name="tran"></param> /// <returns></returns> public int Execute(string sql, ParamList pl, IDbTransaction tran = null) { DBAdapter adapter = null;; try { if (tran == null) { adapter = GetDBAdapter(); } var conn = tran != null ? tran.Connection : adapter.OpenDatabase(); return(conn.Execute(sql, pl.Parameters, tran)); } catch { throw; } finally { if (adapter != null) { adapter.CloseDatabase(); } } }
/// <summary> /// 同一个sql语句,批量操作多个对象 /// </summary> /// <param name="sql"></param> /// <param name="models"></param> /// <param name="tran"></param> public int Execute(string sql, IEnumerable <object> models, IDbTransaction tran = null) { DBAdapter adapter = null;; try { if (tran == null) { adapter = GetDBAdapter(); } var conn = tran != null ? tran.Connection : adapter.OpenDatabase(); return(conn.Execute(sql, models, tran)); } catch (Exception ex) { throw ex; } finally { if (adapter != null) { adapter.CloseDatabase(); } } }
/// <summary> /// 使用事务批量执行sql语句 /// </summary> /// <param name="sqllist"></param> /// <param name="pl"></param> /// <param name="tran"></param> public void Execute(List <string> sqllist, ParamList pl, IDbTransaction tran = null) { DBAdapter adapter = null;; try { if (tran == null) { adapter = GetDBAdapter(); tran = adapter.BeginTransaction(); } var conn = tran.Connection; foreach (var sql in sqllist) { conn.Execute(sql, pl.Parameters, tran); } if (adapter != null) { adapter.Commit(); } } catch { if (adapter != null) { adapter.Rollback(); } throw; } finally { if (adapter != null) { adapter.CloseDatabase(); } } }
public List <T> Query <T>(string sql, object query = null, IDbTransaction tran = null) { DBAdapter adapter = null;; try { if (tran == null) { adapter = GetDBAdapter(); } var conn = tran != null ? tran.Connection : adapter.OpenDatabase(); var m = conn.Query <T>(sql, query).ToList <T>(); return(m); } catch (Exception ex) { throw ex; } finally { if (adapter != null) { adapter.CloseDatabase(); } } }
/// <summary> /// 返回一个ResultSet 查询 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="pl"></param> /// <param name="tran"></param> /// <returns></returns> public ResultSet <T> GetResultSet <T>(string sql, string OrderBy, ParamList pl, IDbTransaction tran = null) { DBAdapter adapter = GetDBAdapter(); try { var conn = adapter.OpenDatabase(); if (pl.ispage) { string pagesql = adapter.GetPageSql(sql, OrderBy); string countsql = adapter.GetCountSql(sql); var m = conn.Query <T>(pagesql, pl.Parameters, tran).ToList <T>(); var c = conn.ExecuteScalar <int>(countsql, pl.Parameters, tran); return(new ResultSet <T>() { Value = m, Total = c }); } else { var m = conn.Query <T>(sql + " order by " + OrderBy, pl.Parameters, tran).ToList <T>(); return(new ResultSet <T>() { Value = m, Total = m.Count }); } } catch { throw; } finally { adapter.CloseDatabase(); } }