/// <summary> /// 选择记录List /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="entity">实体</param> /// <param name="fields">查询的字段</param> /// <param name="strWhere">where条件</param> /// <param name="order">排序字段</param> /// <param name="para">sqlparametor参数值</param> /// <returns>选择记录List</returns> public static List <T> FillOrder <T>(this T entity, string fields, string strWhere, string order, params object[] para) { List <SqlParameter> paraList = new List <SqlParameter>(); #region where条件参数 MatchCollection mc = Regex.Matches(strWhere, @"\{.+?\}"); for (int i = 0; i < mc.Count; i++) { string paraName = mc[i].Value.Replace("{", "").Replace("}", ""); paraList.Add(new SqlParameter(paraName, para[i])); } #endregion SqlEntityOper seo = new SqlEntityOper(); string strWhere0 = strWhere.Replace("{", "").Replace("}", ""); Type t = typeof(T); PropertyInfo[] pis = t.GetProperties(); string strConn = "Default"; foreach (PropertyInfo pi in pis) { if (pi.Name == "Conn") { strConn = pi.GetValue(entity, null) + ""; } } string strSql = string.Format("select {0} from {1} where {2} order by {3}", fields, t.Name, strWhere0, order); DataTable dt = SqlHelper.DataSet(strSql, SqlHelper.CreateConn(strConn), paraList); return(DataTableToList <T>(dt)); }
/// <summary> /// 取某个字段的最大值 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="entity">实体</param> /// <param name="field">字段</param> /// <param name="strWhere">where条件</param> /// <param name="para">where条件参数</param> /// <returns>取某个字段的最大值</returns> public static string Max <T>(this T entity, string field, string strWhere, params object[] para) { List <SqlParameter> paraList = new List <SqlParameter>(); #region where条件参数 MatchCollection mc = Regex.Matches(strWhere, @"\{.+?\}"); for (int i = 0; i < mc.Count; i++) { string paraName = mc[i].Value.Replace("{", "").Replace("}", ""); paraList.Add(new SqlParameter(paraName, para[i])); } #endregion SqlEntityOper seo = new SqlEntityOper(); string strWhere0 = strWhere.Replace("{", "").Replace("}", ""); Type t = typeof(T); PropertyInfo[] pis = t.GetProperties(); string strConn = "Default"; foreach (PropertyInfo pi in pis) { if (pi.Name == "Conn") { strConn = pi.GetValue(entity, null) + ""; } } string strSql = string.Format("select ISNULL(max({0}),0) from {1} where {2}", field, t.Name, strWhere0); string maxValue = SqlHelper.ExecuteScalar(strSql, SqlHelper.CreateConn(strConn), paraList); return(maxValue); }
/// <summary> /// 更新 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="entity">实体</param> /// <param name="strWhere">更新条件 f0={@f0} and f1={@f1}</param> /// <param name="para">条件值</param> /// <returns>影响行数</returns> public static int Update <T>(this T entity, string strWhere, params object[] para) { List <SqlParameter> paraList = new List <SqlParameter>(); #region where条件参数 MatchCollection mc = Regex.Matches(strWhere, @"\{.+?\}"); for (int i = 0; i < mc.Count; i++) { string paraName = mc[i].Value.Replace("{", "").Replace("}", ""); paraList.Add(new SqlParameter(paraName, para[i])); } #endregion SqlEntityOper seo = new SqlEntityOper(); string strConn = ""; string strSql = seo.JoneSqlString(entity, DbType.Update, strWhere, ref strConn, ref paraList); return(SqlHelper.ExecuteNonQuery(strSql, strConn, paraList)); }
/// <summary> /// 执行事务 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="entity">实体</param> /// <param name="conn">数据库连接</param> /// <param name="sqlTran">sql事务</param> /// <returns>影响行数</returns> public static int Insert <T>(this T entity, SqlConnection conn, SqlTransaction sqlTran) where T : new() { SqlEntityOper seo = new SqlEntityOper(); return(seo.ExecuteNonquery(entity, DbType.Insert, sqlTran, true, conn)); }
/// <summary> /// 添加 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="entity">实体</param> /// <returns>影响行数</returns> public static int Insert <T>(this T entity) where T : new() { SqlEntityOper seo = new SqlEntityOper(); return(seo.ExecuteNonquery(entity, DbType.Insert)); }