/// <summary> /// 根据参数数量循环调用方法(隐式事务) /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dao">扩展的类</param> /// <param name="method">要调用的方法名</param> /// <param name="param">方法的参数集合(根据参数数量循环执行方法)</param> /// <returns>是否全部执行成功</returns> public static bool CallMethod_TranScope <T>(this DaoTemplate dao, Func <T, int> method, params T[] param) { bool ret = false; //using语句可保证Dispose() using (TransactionScope tran = new TransactionScope()) { try { foreach (T t in param) { method(t); } tran.Complete(); ret = true; //TransactionScope其实就是:最后调用Complete方法后才提交给数据库 } catch (Exception ex) { //写错误日志 ret = false; } } return(ret); }
/// <summary> /// 根据参数数量循环调用方法(显示事务) /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dao">扩展的类</param> /// <param name="method">要调用的方法名</param> /// <param name="param">方法的参数集合(根据参数数量循环执行方法)</param> /// <returns>是否全部执行成功</returns> public static bool CallMethod_TranCommit <T>(this DaoTemplate dao, Func <T, int> method, params T[] param) { bool ret = false; Transaction oldAmbient = Transaction.Current; CommittableTransaction tran = new CommittableTransaction(); Transaction.Current = tran; try { foreach (T t in param) { method(t); } tran.Commit(); ret = true; } catch { tran.Rollback(); ret = false; } finally { Transaction.Current = oldAmbient; } return(ret); }
public static int CallMethod <T>(this DaoTemplate dao, Func <T, int> method, T param) { int ret = -1; try { ret = method(param); } catch (Exception ex) { //写日志抛错误 //Console.WriteLine(ex.ToString()); } return(ret); }
/// <summary> /// 扩展方法:执行原始SQL命令 /// </summary> /// <param name="dao">扩展的类型</param> /// <param name="commandText">SQL命令</param> /// <param name="parameters">参数</param> /// <returns>影响的记录数</returns> public static int ExecuteSqlNonQuery(this DaoTemplate dao, string commandText, params DbParameter[] parameters) { return(dao.entityModel.ExecuteStoreCommand(commandText, parameters)); }
//扩展方法必须在非泛型的静态类中=扩展类必须是静态and非泛型 //扩展方法必须是静态的 /// <summary> /// 扩展方法:执行原始SQL查询 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dao">扩展的类型</param> /// <param name="commandText">要执行查询的SQL语句</param> /// <param name="parameters">参数</param> /// <returns>返回符合条件的对象的集合</returns> public static ICollection <T> ExecuteSqlQuery <T>(this DaoTemplate dao, string commandText, params DbParameter[] parameters) { return(dao.entityModel.ExecuteStoreQuery <T>(commandText, parameters).ToList <T>()); }