sqldatareader 转实体类 方法1
2010-06-09 21:32
190 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Data;
/// <summary>
/// Summary description for ConvertEntity1
/// </summary>
public class ConvertEntity1
{
public ConvertEntity1()
{
//
// TODO: Add constructor logic here
//
}
public static T ReaderToModel<T>(IDataReader dr)
{
try
{
using (dr)
{
if (dr.Read())
{
Type modelType = typeof(T);
T model = Activator.CreateInstance<T>();
for (int i = 0; i < dr.FieldCount; i++)
{//, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase
PropertyInfo pi = modelType.GetProperty(GetPropertyName(dr.GetName(i)));
if (pi != null)
{
pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
}
}
return model;
}
}
return default(T);
}
catch (Exception ex)
{
return default(T);
}
}
public static List<T> ReaderToList<T>(IDataReader dr)
{
using (dr)
{
List<T> list = new List<T>();
Type modelType = typeof(T);
T model = Activator.CreateInstance<T>();
while (dr.Read())
{
for (int i = 0; i < dr.FieldCount; i++)
{
PropertyInfo pi = modelType.GetProperty(GetPropertyName(dr.GetName(i)), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (pi != null)
{
pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
}
}
list.Add(model);
}
return list;
}
}
//这个类对可空类型进行判断转换,要不然会报错
private static object HackType(object value, Type conversionType)
{
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
return null;
System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
conversionType = nullableConverter.UnderlyingType;
}
return Convert.ChangeType(value, conversionType);
}
private static bool IsNullOrDBNull(object obj)
{
return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false;
}
//取得DB的列对应bean的属性名
private static string GetPropertyName(string column)
{
column = column.ToLower();
string[] narr = column.Split('_');
column = "";
for (int i = 0; i < narr.Length; i++)
{
if (narr[i].Length > 1)
{
column += narr[i].Substring(0, 1).ToUpper() + narr[i].Substring(1);
}
else
{
column += narr[i].Substring(0, 1).ToUpper();
}
}
return column;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Data;
/// <summary>
/// Summary description for ConvertEntity1
/// </summary>
public class ConvertEntity1
{
public ConvertEntity1()
{
//
// TODO: Add constructor logic here
//
}
public static T ReaderToModel<T>(IDataReader dr)
{
try
{
using (dr)
{
if (dr.Read())
{
Type modelType = typeof(T);
T model = Activator.CreateInstance<T>();
for (int i = 0; i < dr.FieldCount; i++)
{//, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase
PropertyInfo pi = modelType.GetProperty(GetPropertyName(dr.GetName(i)));
if (pi != null)
{
pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
}
}
return model;
}
}
return default(T);
}
catch (Exception ex)
{
return default(T);
}
}
public static List<T> ReaderToList<T>(IDataReader dr)
{
using (dr)
{
List<T> list = new List<T>();
Type modelType = typeof(T);
T model = Activator.CreateInstance<T>();
while (dr.Read())
{
for (int i = 0; i < dr.FieldCount; i++)
{
PropertyInfo pi = modelType.GetProperty(GetPropertyName(dr.GetName(i)), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (pi != null)
{
pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
}
}
list.Add(model);
}
return list;
}
}
//这个类对可空类型进行判断转换,要不然会报错
private static object HackType(object value, Type conversionType)
{
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
return null;
System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
conversionType = nullableConverter.UnderlyingType;
}
return Convert.ChangeType(value, conversionType);
}
private static bool IsNullOrDBNull(object obj)
{
return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false;
}
//取得DB的列对应bean的属性名
private static string GetPropertyName(string column)
{
column = column.ToLower();
string[] narr = column.Split('_');
column = "";
for (int i = 0; i < narr.Length; i++)
{
if (narr[i].Length > 1)
{
column += narr[i].Substring(0, 1).ToUpper() + narr[i].Substring(1);
}
else
{
column += narr[i].Substring(0, 1).ToUpper();
}
}
return column;
}
}
相关文章推荐
- SqlDataReader 操作 SqlServer image 列的一些实用方法
- asp.net中SqlDataReader获取数据存在的问题解决方法
- SqlDataReader的Read方法
- 当获取相似数据时,使用不同方法调用不同sp,但是使用同一个方法去用IIDataReader或者SqlDataReader读取数据时需要判断column name是否存在。
- 获取sqlDataReader对象Read()方法读取流的字段值的四种方法
- SqlDataReader的合适关闭方法?
- SqlDataReader的合适关闭方法?
- SqlDataReader对象的NextResult方法读取存储过程多个结果集
- SqlDataReader对象的NextResult方法读取存储过程多个结果集
- [网络收集]ASP.NET两种办法给DropDownList绑定SqlDataReader 及在gridview等控件中绑定dropdownlist的简单方法
- sqldatareader 转实体类 2
- SqlDataReader指定转换无效的解决方法
- SqlDataReader属性方法
- SqlDataReader实例 HasRows属性与 Read()方法
- SqlDataReader 判断是否存在某列的方法
- SqlDataReader对象的NextResult方法读取存储过程多个结果集
- SqlDataReader 中的read 方法是什么意思?
- SqlDataReader的合适关闭方法?
- sqldatareader 转实体类 最简单
- sqldatareader 转实体类 不用反射