123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace Ant.ORM
- {
- public class DatabaseReader
- {
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="value"></param>
- /// <param name="conversionType"></param>
- /// <returns></returns>
- public static object HackType(object value, Type conversionType)
- {
- if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
- {
- if (value == null)
- {
- return null;
- }
- NullableConverter converter = new NullableConverter(conversionType);
- conversionType = converter.UnderlyingType;
- }
- return Convert.ChangeType(value, conversionType);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static bool IsNullOrDBNull(object obj)
- {
- return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString()));
- }
- /// <summary>
- /// Reader转换成DataTable
- /// </summary>
- /// <param name="reader"></param>
- /// <returns></returns>
- public static DataTable ReaderToDataTable(IDataReader reader)
- {
- DataTable table = new DataTable("Table");
- int fieldCount = reader.FieldCount;
- for (int i = 0; i < fieldCount; i++)
- {
- table.Columns.Add(reader.GetName(i).ToLower(), reader.GetFieldType(i));
- }
- table.BeginLoadData();
- object[] values = new object[fieldCount];
- while (reader.Read())
- {
- reader.GetValues(values);
- table.LoadDataRow(values, true);
- }
- reader.Close();
- table.EndLoadData();
- return table;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="dr"></param>
- /// <returns></returns>
- public static Hashtable ReaderToHashtable(IDataReader dr)
- {
- Hashtable hashtable = new Hashtable();
- while (dr.Read())
- {
- for (int i = 0; i < dr.FieldCount; i++)
- {
- string str = dr.GetName(i).ToLower();
- hashtable[str] = dr[str];
- }
- }
- return hashtable;
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="dr"></param>
- /// <returns></returns>
- public static List<T> ReaderToList<T>(IDataReader dr)
- {
- using (dr)
- {
- List<string> list = new List<string>(dr.FieldCount);
- for (int i = 0; i < dr.FieldCount; i++)
- {
- list.Add(dr.GetName(i).ToLower());
- }
- List<T> list2 = new List<T>();
- while (dr.Read())
- {
- T local = Activator.CreateInstance<T>();
- foreach (PropertyInfo info in local.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
- {
- if (list.Contains(info.Name.ToLower()) && !IsNullOrDBNull(dr[info.Name]))
- {
- info.SetValue(local, HackType(dr[info.Name], info.PropertyType), null);
- }
- }
- list2.Add(local);
- }
- return list2;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="dr"></param>
- /// <returns></returns>
- public static T ReaderToModel<T>(IDataReader dr)
- {
- T local = Activator.CreateInstance<T>();
- while (dr.Read())
- {
- foreach (PropertyInfo info in local.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
- {
- if (!IsNullOrDBNull(dr[info.Name]))
- {
- info.SetValue(local, HackType(dr[info.Name], info.PropertyType), null);
- }
- }
- }
- return local;
- }
- }
- }
|