//辅助查询 Author:高兵兵
public class QueryHelp
{
#region IList<T> ToList<T>(string cmdText,string connString) where T : new()
/// <summary>
/// 返回一个list
/// </summary>
/// <typeparam name="T">实体模型</typeparam>
/// <param name="cmdText">存储过程名</param>
/// <param name="connString">连接字符串</param>
/// <returns></returns>
public static IList<T> ToList<T>(string cmdText, string connString) where T : new()
{
using (var read = DbHelp.Create().ExecuteDataReader(cmdText, connString))
{
IList<T> list = null;
var type = typeof(T);
if (read.HasRows)
{ list = new List<T>();
}
while (read.Read())
{
T t = new T();
foreach (PropertyInfo item in type.GetProperties())
{
for (int i = ; i < read.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != read.GetName(i).ToLower()) continue;
var value = read[i];
if (value != DBNull.Value)
{
item.SetValue(t, value, null);
}
break;
}
}
//将创建的对象添加到集合中
list.Add(t);
}
return list;
}
}
#endregion #region IList<T> ToList<T>(string cmdText, List<DbParameter> listpar, string connString) where T : class, new()
/// <summary>
/// 返回一个list
/// </summary>
/// <typeparam name="T">实体模型</typeparam>
/// <param name="cmdText">存储过程名</param>
/// <param name="listpar">参数列表</param>
/// <param name="connString">连接字符串</param>
/// <returns></returns>
public static IList<T> ToList<T>(string cmdText, List<DbParameter> listpar, string connString) where T : class, new()
{
using (var read = DbHelp.Create().ExecuteDataReader(cmdText, listpar, connString))
{
List<T> list = null;
var type = typeof(T);
if (read.HasRows)
{ list = new List<T>();
}
while (read.Read())
{
T t = new T();
foreach (PropertyInfo item in type.GetProperties())
{
for (int i = ; i < read.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != read.GetName(i).ToLower()) continue;
object value = read[i];
if (value != DBNull.Value)
{
item.SetValue(t, value, null);
}
break;
}
}
//将创建的对象添加到集合中
list.Add(t);
}
return list;
}
}
#endregion #region IList<T> ToListAsPager<T>(string cmdText, List<DbParameter> listpar, string connString) where T : class, new()
/// <summary>
/// 返回一个list
/// </summary>
/// <typeparam name="T">实体模型</typeparam>
/// <param name="cmdText">存储过程名</param>
/// <param name="listpar">参数列表</param>
/// <param name="connString">连接字符串</param>
/// <returns></returns>
public static IList<T> ToListAsPager<T>(string cmdText, List<DbParameter> listpar, string connString) where T : class, new()
{
listpar[listpar.Count-].Direction = ParameterDirection.Output;
using (var read = DbHelp.Create().ExecuteDataReader(cmdText, listpar, connString))
{
List<T> list = null;
var type = typeof(T);
if (read.HasRows)
{ list = new List<T>();
}
while (read.Read())
{
T t = new T();
foreach (PropertyInfo item in type.GetProperties())
{
for (int i = ; i < read.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != read.GetName(i).ToLower()) continue;
object value = read[i];
if (value != DBNull.Value)
{
item.SetValue(t, value, null);
}
break;
}
}
//将创建的对象添加到集合中
list.Add(t);
}
return list;
}
}
#endregion #region T FirstOrDefault<T>(string cmdText, string connString)
/// <summary>
/// 返回一个实体模型
/// </summary>
/// <typeparam name="T">实体模型</typeparam>
/// <param name="cmdText">存储过程名</param>
/// <param name="connString">连接字符串</param>
/// <returns></returns>
public static T FirstOrDefault<T>(string cmdText, string connString) where T : class,new()
{
using (var read = DbHelp.Create().ExecuteDataReader(cmdText, connString))
{ Type type = typeof(T); if (!read.Read()) return null;
T t = new T();
foreach (PropertyInfo item in type.GetProperties())
{
for (int i = ; i < read.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != read.GetName(i).ToLower()) continue;
object value = read[i];
if (value != DBNull.Value)
{
item.SetValue(t, value, null);
}
break;
} }
return t;
}
}
#endregion #region T FirstOrDefault<T>(string cmdText, List<DbParameter> list, string connString)
/// <summary>
/// 返回一个实体
/// </summary>
/// <typeparam name="T">实体模型</typeparam>
/// <param name="cmdText">存储过程名称</param>
/// <param name="ob">object[]</param>
/// <param name="connString">连接字符串</param>
/// <returns></returns>
public static T FirstOrDefault<T>(string cmdText, List<DbParameter> list, string connString) where T : class,new()
{
using (var read = DbHelp.Create().ExecuteDataReader(cmdText, list, connString))
{
var type = typeof(T);
if (!read.Read()) return null;
var t = new T();
foreach (var item in type.GetProperties())
{
for (var i = ; i < read.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != read.GetName(i).ToLower()) continue;
var value = read[i];
if (value != DBNull.Value)
{
item.SetValue(t, value, null);
}
break;
}
}
return t;
}
}
#endregion }
QueryHelp