using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace NFine.Data.Extensions
{
public class DatabaseReader
{
///
/// 将IDataReader转换为DataTable
///
///
///
public static DataTable ReaderToDataTable(IDataReader dr)
{
using (dr)
{
DataTable objDataTable = new DataTable("Table");
int intFieldCount = dr.FieldCount;
for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
{
// 2017-2-21 列名被改成小写
//objDataTable.Columns.Add(dr.GetName(intCounter).ToLower(), dr.GetFieldType(intCounter));
objDataTable.Columns.Add(dr.GetName(intCounter), dr.GetFieldType(intCounter));
}
objDataTable.BeginLoadData();
object[] objValues = new object[intFieldCount];
while (dr.Read())
{
dr.GetValues(objValues);
objDataTable.LoadDataRow(objValues, true);
}
dr.Close();
objDataTable.EndLoadData();
return objDataTable;
}
}
///
/// 将IDataReader转换为 集合
///
///
///
///
public static List ReaderToList(IDataReader dr)
{
using (dr)
{
List field = new List(dr.FieldCount);
for (int i = 0; i < dr.FieldCount; i++)
{
//field.Add(dr.GetName(i).ToLower());
field.Add(dr.GetName(i));
}
List list = new List();
while (dr.Read())
{
T model = Activator.CreateInstance();
foreach (PropertyInfo property in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
//if (field.Contains(property.Name.ToLower()))
if (field.Contains(property.Name))
{
if (!IsNullOrDBNull(dr[property.Name]))
{
property.SetValue(model, HackType(dr[property.Name], property.PropertyType), null);
}
}
}
list.Add(model);
}
dr.Close();
return list;
}
}
///
/// 将IDataReader转换为 实体类
///
///
///
///
public static T ReaderToModel(IDataReader dr)
{
using (dr)
{
T model = Activator.CreateInstance();
while (dr.Read())
{
foreach (PropertyInfo pi in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
{
if (!IsNullOrDBNull(dr[pi.Name]))
{
pi.SetValue(model, HackType(dr[pi.Name], pi.PropertyType), null);
}
}
}
dr.Close();
return model;
}
}
///
/// 将IDataReader转换为 哈希表
///
///
///
public static Hashtable ReaderToHashtable(IDataReader dr)
{
using (dr)
{
Hashtable ht = new Hashtable();
while (dr.Read())
{
for (int i = 0; i < dr.FieldCount; i++)
{
//string strfield = dr.GetName(i).ToLower();
string strfield = dr.GetName(i);
ht[strfield] = dr[strfield];
}
}
dr.Close();
return ht;
}
}
//这个类对可空类型进行判断转换,要不然会报错
public 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);
}
public static bool IsNullOrDBNull(object obj)
{
return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false;
}
}
}