/// <summary> /// 执行存储过程返回DataTable /// </summary> /// <param name="proc">存储过程</param> /// <returns>返回DataTable</returns> public DataTable ExecuteDataTable(Proc proc) { try { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = proc.ProcName; if (con.State == System.Data.ConnectionState.Closed) { con.Open(); } cmd.Parameters.AddRange(proc.InitParameters(cmd.CreateParameter)); DbDataAdapter da = objFactory.CreateDataAdapter(); da.SelectCommand = cmd; DataTable dt = new DataTable(); da.Fill(dt); return(dt); } catch (DbException e) { Exception.Log(e, proc.ProcName, cmd.Parameters); return(null); } finally { proc.InitOutput(); cmd.Parameters.Clear(); if (!isOpenTran) { Close(); } } }
/// <summary> /// 执行Command设置好所有参数 /// </summary> /// <typeparam name="T">返回类型参数</typeparam> /// <param name="proc">存储过程</param> /// <param name="execute">执行的那个方法</param> /// <returns>返回指定参数</returns> private T ExecuteCmd <T>(Proc proc, Execute <T> execute) { try { cmd.CommandText = proc.ProcName; cmd.CommandType = CommandType.StoredProcedure; if (con.State == System.Data.ConnectionState.Closed) { con.Open(); } cmd.Parameters.AddRange(proc.InitParameters(cmd.CreateParameter)); return(execute()); } catch (DbException e) { Exception.Log(e, proc.ProcName, cmd.Parameters); return(default(T)); } finally { proc.InitOutput(); cmd.Parameters.Clear(); if (!isOpenTran && !typeof(T).IsAssignableFrom(typeof(DbDataReader))) { Close(); } } }