@manhng

Welcome to my blog!

DataReaderExtension

October 6, 2017 01:08

http://www.dotnetstudy.com/DataReader-Extension-to-map-DataReader-with-object-or-list-of-objects?id=17

http://www.ravikumargupta.com/2012/11/some-useful-datareader-extension-methods.html

https://github.com/StackExchange/Dapper

DataReaderExtension.cs

 

public static class DataReaderExtensions
{
    /// <Summary>
    /// Map data from DataReader to list of objects
    /// </Summary>
    /// <typeparam name="T">Object</typeparam>
    /// <param name="dr">Data Reader</param>
    /// <returns>List of objects having data from data reader</returns>
    public static List<T> MapToList<T>(this DbDataReader dr) where T : new()
    {
        List<T> RetVal = null;
        var Entity = typeof(T);
        var PropDict = new Dictionary<string, PropertyInfo>();
        try
        {
            if (dr != null && dr.HasRows)
            {
                RetVal = new List<T>();
                var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);
                while (dr.Read())
                {
                    T newObject = new T();
                    for (int Index = 0; Index < dr.FieldCount; Index++)
                    {
                        if (PropDict.ContainsKey(dr.GetName(Index).ToUpper()))
                        {
                            var Info = PropDict[dr.GetName(Index).ToUpper()];
                            if ((Info != null) && Info.CanWrite)
                            {
                                var Val = dr.GetValue(Index);
                                Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val, null);
                            }
                        }
                    }
                    RetVal.Add(newObject);
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
        return RetVal;
    }
    /// <Summary>
    /// Map data from DataReader to an object
    /// </Summary>
    /// <typeparam name="T">Object</typeparam>
    /// <param name="dr">Data Reader</param>
    /// <returns>Object having data from Data Reader</returns>
    public static T MapToSingle<T>(this DbDataReader dr) where T : new()
    {
        T RetVal = new T();
        var Entity = typeof(T);
        var PropDict = new Dictionary<string, PropertyInfo>();
        try
        {
            if (dr != null && dr.HasRows)
            {
                var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);
                dr.Read();
                for (int Index = 0; Index < dr.FieldCount; Index++)
                {
                    if (PropDict.ContainsKey(dr.GetName(Index).ToUpper()))
                    {
                        var Info = PropDict[dr.GetName(Index).ToUpper()];
                        if ((Info != null) && Info.CanWrite)
                        {
                            var Val = dr.GetValue(Index);
                            Info.SetValue(RetVal, (Val == DBNull.Value) ? null : Val, null);
                        }
                    }
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
        return RetVal;
    }
}

TConverter.cs

public static class TConverter
{
    public static T ChangeType<T>(object value)
    {
        return (T)ChangeType(typeof(T), value);
    }
    public static object ChangeType(Type t, object value)
    {
        TypeConverter tc = TypeDescriptor.GetConverter(t);
        return tc.ConvertFrom(value);
    }
}

How to use?



Map data from DataReader to single object

dr = cmd.ExecuteReader();
objCustomer = dr.MapToSingle<Customer>();

Map data from DataReader to list of object

dr = cmd.ExecuteReader();
objCustomerList = dr.MapToList<Customer>();

Categories

Recent posts