/// <summary> /// 判断重复(根据主键) /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="id">主键</param> /// <returns>是否存在</returns> public Enums.TickLingEnum ExistsByID <T>(string id) { try { Type typeFromHandle = typeof(T); string strTableName = typeFromHandle.Name; var parems = new DynamicParameters(); string strwhere = string.Empty; //捕获主键 foreach (PropertyInfo item in typeFromHandle.GetProperties()) { if (item.IsDefined(typeof(PrimaryKeyAttribute), false) && !string.IsNullOrEmpty(id)) { strwhere = string.Format("where {0} = '{1}'", item.Name, id); parems.Add("@" + item.Name, id); //添加主键参数 } } string strSql = string.Format("select count(*) from [" + strTableName + "] {0}", strwhere); using (DbConnection Connection = new DBFactory().GetInstance()) { Connection.Open(); return(Convert.ToInt32(Connection.Query <int>(strSql, parems, null, false, null, CommandType.Text).ToList()[0]) > 0 ? Enums.TickLingEnum.Existence : Enums.TickLingEnum.NonExistence); } } catch (Exception ex) { return(Enums.TickLingEnum.Abnormity); } }
/// <summary> /// 判断重复(根据条件) /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="columnTable">条件集合</param> /// <returns></returns> public Enums.TickLingEnum CheckColumnValueIsExist <T>(Hashtable columnTable) { try { Type typeFromHandle = typeof(T); string strTableName = typeFromHandle.Name; var parems = new DynamicParameters(); StringBuilder sbwhere = new StringBuilder(); if (columnTable.Count > 0) { sbwhere.Append(" where "); foreach (DictionaryEntry item in columnTable) { sbwhere.AppendFormat(" {0} = @{1} and", item.Key, item.Key); parems.Add("@" + item.Key, item.Value.ToString().Trim()); //添加主键参数 } sbwhere.Remove(sbwhere.Length - 3, 3); } string strSql = string.Format("select count(*) from [" + strTableName + "] {0}", sbwhere); using (DbConnection Connection = new DBFactory().GetInstance()) { Connection.Open(); return(Convert.ToInt32(Connection.Query <int>(strSql, parems, null, false, null, CommandType.Text).ToList()[0]) > 0 ? Enums.TickLingEnum.Existence : Enums.TickLingEnum.NonExistence); } } catch (Exception ex) { return(Enums.TickLingEnum.Abnormity); } }
/// <summary> /// 根据输入参数和输出参数的存储过程 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="storedProcedureName">存储过程名称</param> /// <param name="normalParas">普通参数 键值对</param> /// <param name="outPutParas">输出参数 键值对</param> /// <returns>查询结果泛型集合</returns> public List <T> SelectByStoredProcedure <T>(string storedProcedureName, Hashtable normalParms, ref Hashtable outPutParms) { try { using (DbConnection Connection = new DBFactory().GetInstance()) { var parems = new DynamicParameters(); //添加普通参数 if (normalParms != null) { foreach (DictionaryEntry item in normalParms) { parems.Add("@" + item.Key, item.Value); } } //添加返回参数 if (outPutParms != null) { foreach (DictionaryEntry item in outPutParms) { //需要后期判断参数类型 得到 DbType parems.Add("@" + item.Key, item.Value, DbType.Int32, ParameterDirection.Output); //这样写返回值可能会出错 //parems.Add("@" + item.Key, ParameterDirection.Output); } } List <T> lists = Connection.Query <T>(storedProcedureName, parems, null, true, null, CommandType.StoredProcedure).ToList(); string[] keys = new string[outPutParms.Keys.Count]; outPutParms.Keys.CopyTo(keys, 0); foreach (string item in keys) { outPutParms[item] = parems.Get <int>("@" + item); } return(lists); } } catch (Exception) { return(null); } }
/// <summary> /// 根据输入参数和输出参数的存储过程 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="storedProcedureName">存储过程名称</param> /// <param name="normalParas">普通参数 键值对</param> /// <returns>查询结果泛型集合</returns> public List <T> SelectByStoredProcedure <T>(string storedProcedureName, Hashtable normalParms) { try { using (DbConnection Connection = new DBFactory().GetInstance()) { var parems = new DynamicParameters(); //添加普通参数 if (normalParms != null) { foreach (DictionaryEntry item in normalParms) { parems.Add("@" + item.Key, item.Value); } } List <T> lists = Connection.Query <T>(storedProcedureName, parems, null, true, null, CommandType.StoredProcedure).ToList(); return(lists); } } catch (Exception ex) { return(null); } }
/// <summary> /// 判断重复 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="id">主键</param> /// <returns>是否存在</returns> public Enums.TickLingEnum IsExists <T>(string where, string id) { try { Type typeFromHandle = typeof(T); string strTableName = typeFromHandle.Name; string strwhere = where; if (!string.IsNullOrEmpty(id)) { strwhere += " and id='" + id + "'"; } string strSql = string.Format("select count(*) from [" + strTableName + "] where {0}", strwhere); using (DbConnection Connection = new DBFactory().GetInstance()) { Connection.Open(); return(Convert.ToInt32(Connection.Query <int>(strSql, null, null, false, null, CommandType.Text).ToList()[0]) > 0 ? Enums.TickLingEnum.Existence : Enums.TickLingEnum.NonExistence); } } catch (Exception ex) { return(Enums.TickLingEnum.Abnormity); } }