public override IList <T> Select(string tran, IList <OrderBy> orders, Limit limit, out string message, params Restrain[] restrain) { DboProjectionAttribute tnAttr = typeof(T).GetCustomAttribute <DboProjectionAttribute>(); if (tnAttr == null || String.IsNullOrWhiteSpace(tnAttr.Projection)) { throw new Exception(String.Format("对象{0}没有设置表映射关系", typeof(T).Name)); } IList <T> result = new List <T>(); StringBuilder restainStr = new StringBuilder(); SqlCommand sqlCmd = new SqlCommand(); foreach (Restrain rst in restrain) { if (rst == null) { continue; } restainStr.Append(rst.SqlStr); foreach (KeyValuePair <string, object> kv in rst.Params) { SqlParameter param = new SqlParameter { ParameterName = kv.Key.Replace("__PARAMCODE__", "@"), Value = kv.Value }; sqlCmd.Parameters.Add(param); } } StringBuilder ordBy = new StringBuilder(); PropertyInfo[] pis = typeof(T).GetProperties(); IEnumerable <string> piNames = from pi in pis select pi.Name; ordBy.Append(" ORDER BY "); if (orders != null) { foreach (OrderBy order in orders) { if (String.IsNullOrWhiteSpace(order.Field)) { throw new ArgumentNullException(); } if (piNames.Contains(order.Field)) { ordBy.AppendFormat(" {0}", order.Field); } if (!order.Asc) { ordBy.Append(" DESC"); } else { ordBy.Append(" ASC"); } ordBy.Append(" ,"); } } if (ordBy[ordBy.Length - 1] == ',') { ordBy.Remove(ordBy.Length - 1, 1); } SqlTransaction sqlTran = null; bool runInTran = !String.IsNullOrWhiteSpace(tran); if (runInTran) { sqlTran = (SqlTransaction)SqlProvider.GetTransaction(tran); } else { tran = SqlProvider.CreateTransaction <T>(); sqlTran = (SqlTransaction)SqlProvider.GetTransaction(tran); } if (sqlTran == null && runInTran) { message = String.Format("Trans={0}不存在", tran); return(new List <T>()); } if (limit == null) { limit = new Limit(); } string sql = "WITH _T_ AS ((SELECT TOP {1} ROW_NUMBER() OVER({2}) AS _AUTOID_, * FROM {3} WHERE 1 = 1 {5}))SELECT TOP {0} * FROM _T_ {6} WHERE _AUTOID_ > {4}"; sqlCmd.CommandText = String.Format(sql, limit.Size, limit.Size + limit.Pos, ordBy.ToString(), tnAttr.Projection, limit.Pos, restainStr, runInTran?"":"WITH(NOLOCK)") .Replace("__PARAMCODE__", "@"); sqlCmd.Transaction = sqlTran; sqlCmd.Connection = sqlTran.Connection; Debug(sqlCmd); try { SqlDataReader reader = sqlCmd.ExecuteReader(); while (reader.Read()) { T item = new T(); PropertyInfo[] pList = typeof(T).GetProperties(); foreach (PropertyInfo pi in pList) { if (pi.GetCustomAttribute <NotColumnAttribute>() != null) { continue; } object val = reader[pi.Name]; if (val.GetType() != typeof(DBNull)) { pi.SetValue(item, val); } } result.Add(item); } reader.Close(); if (!runInTran) { SqlProvider.CloseTransaction(tran, true); } message = String.Format("列出对象{0}笔", result.Count); } catch (Exception ex) { message = ex.Message; SqlProvider.CloseTransaction(tran, false); } return(result); }
/// <summary> /// 从数据库中取得制定条件的对象 /// </summary> /// <param name="tran">事务号</param> /// <param name="order">排序信息</param> /// <param name="limit">分页信息</param> /// <param name="message">提示信息</param> /// <param name="restrain">条件</param> /// <returns>对象列表</returns> public abstract IList <T> Select(string tran, IList <OrderBy> order, Limit limit, out string message, params Restrain[] restrain);