1. 程式人生 > >c# 將IDataReader轉換為DataTable

c# 將IDataReader轉換為DataTable

    /// <summary>
    ///  將IDataReader轉換為DataTable
    /// </summary>
    /// <param name="reader"></param>
    /// <returns></returns>
    public static DataTable DataTableToIDataReader(IDataReader reader)
    {
        DataTable objDataTable = new DataTable("Table");
        int intFieldCount = reader.FieldCount;
        for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
        {
            objDataTable.Columns.Add(reader.GetName(intCounter).ToUpper(), reader.GetFieldType(intCounter));
        }
        objDataTable.BeginLoadData();
        object[] objValues = new object[intFieldCount];
        while (reader.Read())
        {
            reader.GetValues(objValues);
            objDataTable.LoadDataRow(objValues, true);
        }
        reader.Close();
        objDataTable.EndLoadData();
        return objDataTable;
    }
 
    public static T ReaderToModel<T>(IDataReader dr)
    {
        try
        {
            using (dr)
            {
                if (dr.Read())
                {
                    List<string> list = new List<string>(dr.FieldCount);
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        list.Add(dr.GetName(i).ToLower());
                    }
                    T model = Activator.CreateInstance<T>();
                    foreach (PropertyInfo pi in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
                    {
                        if (list.Contains(pi.Name.ToLower()))
                        {
                            if (!IsNullOrDBNull(dr[pi.Name]))
                            {
                                pi.SetValue(model, HackType(dr[pi.Name], pi.PropertyType), null);
                            }
                        }
                    }
                    return model;
                }
            }
            return default(T);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
 
    public static List<T> ReaderToList<T>(IDataReader dr)
    {
        using (dr)
        {
            List<string> field = new List<string>(dr.FieldCount);
            for (int i = 0; i < dr.FieldCount; i++)
            {
                field.Add(dr.GetName(i).ToLower());
            }
            List<T> list = new List<T>();
            while (dr.Read())
            {
                T model = Activator.CreateInstance<T>();
                foreach (PropertyInfo property in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance))
                {
                    if (field.Contains(property.Name.ToLower()))
                    {
                        if (!IsNullOrDBNull(dr[property.Name]))
                        {
                            property.SetValue(model, HackType(dr[property.Name], property.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;
    }
}