public virtual bool Insert(TEntity instance) { var Connection = MyConnection.Pop(); bool added; var queryResult = SqlGenerator.GetInsert(instance); if (SqlGenerator.IsIdentity) { var newId = Connection.Query <long>(queryResult.Sql, queryResult.Param).FirstOrDefault(); added = newId > 0; if (added) { var newParsedId = Convert.ChangeType(newId, SqlGenerator.IdentityProperty.PropertyInfo.PropertyType); SqlGenerator.IdentityProperty.PropertyInfo.SetValue(instance, newParsedId); } } else { added = Connection.Execute(queryResult.Sql, instance) > 0; } MyConnection.Push(Connection); return(added); }
public int TestTrans(tbl_Account model) { //事务1:取出数据库连接 var conn = MyConnection.Pop(); //事务1:开启事物 IDbTransaction tran = conn.BeginTransaction(); //事务1:拼接成SQL var sqlParam = SqlGenerator.GetInsert(model); //事务1:执行事务1 conn.Execute(sqlParam.Sql, sqlParam.Param, tran); //分表分库so new var dapperR = new DapperRepository <tbl_PublicAccountRepository>(); var conn2 = dapperR.MyConnection.Pop(); IDbTransaction tran2 = conn2.BeginTransaction(); //事务2:拼接成SQL var sqlParam2 = SqlGenerator.GetInsert(model); //事务2:执行事务1 conn.Execute(sqlParam.Sql, sqlParam2.Param, tran); //要么一起提交 ,要么一起回滚 tran.Commit(); tran2.Commit(); //事务1:返回数据库 MyConnection.Push(conn); //事务2:返回数据库 dapperR.MyConnection.Push(conn); return(0); }
public virtual IEnumerable <TEntity> FindAll(SqlQuery sqlQuery) { var Connection = MyConnection.Pop(); var result = Connection.Query <TEntity>(sqlQuery.Sql, sqlQuery.Param); MyConnection.Push(Connection); return(result); }
private bool Update(SqlQuery query) { var Connection = MyConnection.Pop(); var updated = Connection.Execute(query.Sql, query.Param) > 0; MyConnection.Push(Connection); return(updated); }
public virtual bool Delete(SqlQuery queryResult) { var Connection = MyConnection.Pop(); var deleted = Connection.Execute(queryResult.Sql, queryResult.Param) > 0; MyConnection.Push(Connection); return(deleted); }
public virtual IEnumerable <TEntity> FindAll(Expression <Func <TEntity, bool> > expression) { var queryResult = SqlGenerator.GetSelectAll(expression); var Connection = MyConnection.Pop(); var result = Connection.Query <TEntity>(queryResult.Sql, queryResult.Param); MyConnection.Push(Connection); return(result); }
public IEnumerable <TEntity> FindAllBetween(object from, object to, Expression <Func <TEntity, object> > btwField, Expression <Func <TEntity, bool> > expression) { var queryResult = SqlGenerator.GetSelectBetween(from, to, btwField, expression); var Connection = MyConnection.Pop(); var data = Connection.Query <TEntity>(queryResult.Sql, queryResult.Param); MyConnection.Push(Connection); return(data); }
public PageListView <TEntity> FindAllPages(long from, long to, Expression <Func <TEntity, bool> > expression, Expression <Func <TEntity, object> > field, bool isDesc = false) { var queryResult = SqlGenerator.GetSelectAll(expression); var countResult = SqlGenerator.GetSelectCount(queryResult.Sql, queryResult.Param); var pageResult = SqlGenerator.GetSelectPages(from, to, queryResult.Sql, queryResult.Param, field, isDesc); var Connection = MyConnection.Pop(); var result = new PageListView <TEntity> { PageIndex = from, PageSize = to, DataRows = Convert.ToInt64(Connection.ExecuteScalar(countResult.Sql, countResult.Param)), Data = Connection.Query <TEntity>(pageResult.Sql, pageResult.Param) as List <TEntity> }; MyConnection.Push(Connection); return(result); }