C#通用DAO实现(三)

第481行到998行代码:

 

/// <summary> /// 根据非范型分页对象获得总条数 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pi"></param> /// <returns></returns> public int GetMaxRecodeCount(PaginationInfo pi) { DbConnection dcon = null; DbCommand dcom = null; try { dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); List<Object> objs = new List<Object>(); string SqlStr = @"select count({pkey}) from {tablename} where 1=1 {condition}"; SqlStr = String.IsNullOrEmpty(pi.TableName) ? SqlStr.Replace("{pkey}", GetPrimaryKey(pi.ClassType)) : SqlStr.Replace("{pkey}", GetPrimaryKey<Object>(pi.TableName)); SqlStr = String.IsNullOrEmpty(pi.TableName) ? SqlStr.Replace("{tablename}", "[" + pi.ClassType.Name + "]") : SqlStr.Replace("{tablename}", "[" + pi.TableName + "]"); string conditionStr = ""; if (pi.Conditions != null) { foreach (Connditon condition in pi.Conditions) { if (condition != null) { if (condition.Value != null) { conditionStr += " and " + condition.Name + " " + Connditon.GetType(condition.OpType) + " @" + GetParname(condition); DbParameter par = dcom.CreateParameter(); par.ParameterName = "@" + GetParname(condition); if (condition.OpType != Op.Like) { par.Value = condition.Value; } else { par.Value = "%" + condition.Value + "%"; } par.DbType = GetDbType(pi.ClassType.GetProperty(condition.Name)); dcom.Parameters.Add(par); } } } } dcom.CommandText = SqlStr.Replace("{condition}", conditionStr); int i = (int)dcom.ExecuteScalar(); return i; } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } public int GetMaxRecodeCount(PaginationInfo pi,params String[] tbnames) { if (tbnames == null || tbnames.Length == 0) { throw new ArgumentException("获取总条数时表名不能为空"); } DbConnection dcon = null; DbCommand dcom = null; try { dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); List<Object> objs = new List<Object>(); string sqlUnit = @"select 1 as pkey from {tablename} "+ " where 1=1 {condition} union all "; string sqlAll = ""; string SqlStr = @"select count(1) from ({tablename}) as model"; foreach (String tname in tbnames) { sqlAll += sqlUnit.Replace("{tablename}","["+tname+"]"); } sqlAll = sqlAll.Substring(0,sqlAll.Length-10); SqlStr = SqlStr.Replace("{tablename}",sqlAll); string conditionStr = ""; if (pi.Conditions != null) { foreach (Connditon condition in pi.Conditions) { if (condition != null) { if (condition.Value != null) { conditionStr += " and " + condition.Name + " " + Connditon.GetType(condition.OpType) + " @" + GetParname(condition); DbParameter par = dcom.CreateParameter(); par.ParameterName = "@" + GetParname(condition); if (condition.OpType != Op.Like) { par.Value = condition.Value; } else { par.Value = "%" + condition.Value + "%"; } par.DbType = GetDbType(pi.ClassType.GetProperty(condition.Name)); dcom.Parameters.Add(par); } } } } dcom.CommandText = SqlStr.Replace("{condition}", conditionStr); int i = (int)dcom.ExecuteScalar(); return i; } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } public int ExecuteNonQuery(String sql) { return SqlHelper.ExecuteNonQuery( SqlHelper._connString, CommandType.Text, sql, null ); } /// <summary> /// 执行任意SQL语句 /// (该方法运行在指定的事务上下文当中) /// </summary> /// <param name="ts"></param> /// <param name="sql"></param> /// <returns></returns> public int ExecuteNonQuery(DbTransaction ts,String sql) { int retValue = 0; using (DbCommand cmd = ts.Connection.CreateCommand()) { cmd.CommandText = sql; cmd.Transaction = ts; retValue = cmd.ExecuteNonQuery(); } return retValue; } public List<T> ExecuteQuery<T>(String sql) where T:new() { SqlDataReader reader = null; List<T> list = null; try { reader = SqlHelper.ExecuteReader(sql); list = this.EncapReaderToEntity<T>(reader); } finally { if (reader != null && !reader.IsClosed) { reader.Close(); } } return list; } public List<T> ExecuteQuery<T>(DbTransaction ts,String sql) where T:new() { SqlDataReader reader = null; List<T> list = null; try { using (DbCommand cmd = ts.Connection.CreateCommand()) { cmd.CommandText = sql; cmd.Transaction = ts; reader = (SqlDataReader)cmd.ExecuteReader(); } list = this.EncapReaderToEntity<T>(reader); } finally { if (reader != null && !reader.IsClosed) { reader.Close(); } } return list; } /// <summary> /// 查询获得结果 /// </summary> /// <param name="sql"></param> /// <returns></returns> public String ExecuteScalar(DbTransaction ts, String sql) { SqlDataReader reader = null; String value = null; using (DbCommand cmd = ts.Connection.CreateCommand()) { cmd.CommandText = sql; cmd.Transaction = ts; reader = (SqlDataReader)cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { value = reader[0].ToString(); } } reader.Close(); return value; } } /// <summary> /// 根据条件获取满足条件的记录数 /// </summary> /// <typeparam name="T">实体类名</typeparam> /// <param name="Conditions">条件数组</param> /// <param name="PkPlace">主键名</param> /// <returns></returns> public int GetMaxRecodeCount<T>(Connditon[] Conditions, string PkPlace) { DbConnection dcon = null; DbCommand dcom = null; try { dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); List<T> objs = new List<T>(); string SqlStr = @"select count({pkey}) from {tablename} where 1=1 {condition}"; if (PkPlace == "") { SqlStr = SqlStr.Replace("{pkey}", GetPrimaryKey<T>()); } else { SqlStr = SqlStr.Replace("{pkey}", PkPlace); } SqlStr = SqlStr.Replace("{tablename}", "[" + typeof(T).Name + "]"); string conditionStr = ""; if (Conditions != null) { foreach (Connditon condition in Conditions) { if (condition != null) { if ( condition.Value != null) { conditionStr += " and " + condition.Name + " " + Connditon.GetType(condition.OpType) + " @" + GetParname(condition); DbParameter par = dcom.CreateParameter(); par.ParameterName = "@" + GetParname(condition); if (condition.OpType != Op.Like) { par.Value = condition.Value; } else { par.Value = "%" + condition.Value + "%"; } par.DbType = GetDbType(typeof(T).GetProperty(condition.Name)); dcom.Parameters.Add(par); } } } } dcom.CommandText = SqlStr.Replace("{condition}", conditionStr); int i = (int)dcom.ExecuteScalar(); return i; } catch (Exception ex) { throw ex; } finally { dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } /// <summary> /// 获取属性名 /// </summary> /// <param name="con"></param> /// <returns></returns> public string GetParname(Connditon con) { if (con.OpType == Op.BeginWith || con.OpType == Op.MoreThan) { return "Max" + con.Name; } else if (con.OpType == Op.EndWith || con.OpType == Op.LessThan) { return "Min" + con.Name; } else { return con.Name; } } /// <summary> /// 返回实体类集合 /// </summary> /// <typeparam name="T">实体类名</typeparam> /// <param name="pageIndex">页数</param> /// <param name="countPerPage">总页数</param> /// <param name="Conditions">查询条件(可为null)</param> /// <param name="PkPlace">主键名</param> /// <returns></returns> public List<T> Select<T>(int pageIndex, int countPerPage, Connditon[] Conditions, string PkPlace) where T : new() { DbConnection dcon = null; DbCommand dcom = null; DbDataReader dr = null; try { dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); List<T> objs = new List<T>(); pageIndex = pageIndex <= 0 ? 1 : pageIndex; string SqlStr = @"select top {percount} * from {tablename} where {pkey} not in (select top {passcount} {pkey} from {tablename} where 1=1 {condition} order by {pkey} desc) {condition} order by {pkey} desc"; SqlStr = SqlStr.Replace("{percount}", countPerPage.ToString()); SqlStr = SqlStr.Replace("{passcount}", (countPerPage * (pageIndex - 1)).ToString()); if (PkPlace == "") { SqlStr = SqlStr.Replace("{pkey}", GetPrimaryKey<T>()); } else { SqlStr = SqlStr.Replace("{pkey}", PkPlace); } SqlStr = SqlStr.Replace("{tablename}", "[" + typeof(T).Name + "]"); string conditionStr = ""; if (Conditions != null) { foreach (Connditon condition in Conditions) { if (condition != null) { if (condition.Value != null) { conditionStr += " and " + condition.Name + " " + Connditon.GetType(condition.OpType) + " @" + GetParname(condition); DbParameter par = dcom.CreateParameter(); par.ParameterName = "@" + GetParname(condition); if (condition.OpType != Op.Like) { par.Value = condition.Value; } else { par.Value = "%" + condition.Value + "%"; } par.DbType = GetDbType(typeof(T).GetProperty(condition.Name)); dcom.Parameters.Add(par); } } } } dcom.CommandText = SqlStr.Replace("{condition}", conditionStr); dr = dcom.ExecuteReader(); while (dr.Read()) { T obj = new T(); PropertyInfo[] pros = typeof(T).GetProperties(); foreach (PropertyInfo pro in pros) { if (dr[pro.Name].GetType() != typeof(DBNull)) { pro.SetValue(obj, dr[pro.Name], null); } } objs.Add(obj); } return objs; } catch (Exception ex) { throw ex; } finally { dr.Close(); dcom.Dispose(); dcon.Close(); dcon.Dispose(); } } /// <summary> /// 返回实体类集合 /// </summary> /// <typeparam name="T">实体类名</typeparam> /// <param name="pageIndex">页数</param> /// <param name="countPerPage">总页数</param> /// <param name="Conditions">查询条件(可为null)</param> /// <returns></returns> public List<T> Select<T>(int pageIndex, int countPerPage, Connditon[] Conditions) where T : new() { DbConnection dcon = null; DbCommand dcom = null; DbDataReader dr = null; try { dcon = Conn.getConn(); dcon.Open(); dcom = dcon.CreateCommand(); List<T> objs = new List<T>(); pageIndex = pageIndex <= 0 ? 1 : pageIndex; string SqlStr = @"select top {percount} * from {tablename} where {pkey} not in (select top {passcount} {pkey} from {tablename} where 1=1 {condition} order by {pkey} desc) {condition} order by {pkey} desc"; SqlStr = SqlStr.Replace("{percount}", countPerPage.ToString()); SqlStr = SqlStr.Replace("{passcount}", (countPerPage * (pageIndex - 1)).ToString()); SqlStr = SqlStr.Replace("{pkey}", GetPrimaryKey<T>()); SqlStr = SqlStr.Replace("{tablename}", "[" + typeof(T).Name + "]"); string conditionStr = ""; if (Conditions != null) { foreach (Connditon condition in Conditions) { if (condition != null) { if (condition.Value != null) { conditionStr += " and " + condition.Name + " " + Connditon.GetType(condition.OpType) + " @" + GetParname(condition); DbParameter par = dcom.CreateParameter(); par.ParameterName = "@" + GetParname(condition); if (condition.OpType != Op.Like) { par.Value = condition.Value; } else { par.Value = "%" + condition.Value + "%"; } par.DbType = GetDbType(typeof(T).GetProperty(condition.Name)); dcom.Parameters.Add(par); } } } } dcom.CommandText = SqlStr.Replace("{condition}", conditionStr); dr = dcom.ExecuteReader(); while (dr.Read()) { T obj = new T(); PropertyInfo[] pros = typeof(T).GetProperties(); foreach (PropertyInfo pro in pros) { if (dr[pro.Name].ToString() != "") { if (dr[pro.Name].GetType() != typeof(DBNull)) pro.SetValue(obj, dr[pro.Name], null); } } objs.Add(obj); } return objs; } catch (Exception ex) { throw ex; } finally { dr.Close(); dcom.Dispose(); dcon.Close(); dcon.Dispose(); } }

你可能感兴趣的:(C#通用DAO实现(三))