http://blog.csdn.net/shuizhaoshui/article/details/51425527
在.NET开发中,操作关系型数据库提取数据经常用到DataTable。ASP.NET前后台数据绑定应用DataTable的时候似乎也很多,但是List集合比DataTable应用更加广泛,提取处理数据也更加方便,MVC绑定数据更倾向于List。 因此,我们会经常需要对List集合和DataTable数据进行互转,以下三个方法是实现List和DataTable互转,以及DataTable单行提取对象。好了,直接上代码了:
1、DataTable转List集合
 
-    
 
-    
 
-    
 
-    
 
-    
 
-    
 
-    public static List<T> TableToList<T>(DataTable dt, bool isStoreDB = true)  
 
-    {  
 
-        List<T> list = new List<T>();  
 
-        Type type = typeof(T);  
 
-        List<string> listColums = new List<string>();  
 
-        foreach (DataRow row in dt.Rows)  
 
-        {  
 
-            PropertyInfo[] pArray = type.GetProperties(); 
 
-            T entity = Activator.CreateInstance<T>(); 
 
-            foreach (PropertyInfo p in pArray)  
 
-            {  
 
-                if (!dt.Columns.Contains(p.Name) || row[p.Name] == null || row[p.Name] == DBNull.Value)  
 
-                {  
 
-                    continue;  
 
-                }  
 
-                if (isStoreDB && p.PropertyType == typeof(DateTime) && Convert.ToDateTime(row[p.Name]) < Convert.ToDateTime("1753-01-01"))  
 
-                {  
 
-                    continue;  
 
-                }  
 
-                try  
 
-                {  
 
-                    var obj = Convert.ChangeType(row[p.Name], p.PropertyType);
 
-                    p.SetValue(entity, obj, null);  
 
-                }  
 
-                catch (Exception)  
 
-                {  
 
-                    
 
-                }  
 
-                
 
-                
 
-                
 
-                
 
-                
 
-                
 
-                
 
-                
 
-                
 
-            }  
 
-            list.Add(entity);  
 
-        }  
 
-        return list;  
 
-    }  
 
 
  
 
isStoreDB形参是在考虑List转化的DataTale数据要不要存储数据库,sqlserver数据中,时间类型date和datetime范围不同,date时间范围是在元年1月1日到9999年12月31日,datetime时间范围是在1753年1月1日到9999年12月31日,不在范围内的时间存储到数据库产生异常,因此加上时间限定,默认为存入数据库中数据。
2、List集合转DataTable
 
-       
 
-       
 
-       
 
-       
 
-       
 
-       
 
-       public static DataTable ListToTable<T>(List<T> list, bool isStoreDB = true)  
 
-       {  
 
-           Type tp = typeof(T);  
 
-           PropertyInfo[] proInfos = tp.GetProperties();  
 
-           DataTable dt = new DataTable();  
 
-           foreach (var item in proInfos)  
 
-           {  
 
-               dt.Columns.Add(item.Name, item.PropertyType); 
 
-           }  
 
-           foreach (var item in list)  
 
-           {  
 
-               DataRow dr = dt.NewRow();  
 
-               foreach (var proInfo in proInfos)  
 
-               {  
 
-                   object obj = proInfo.GetValue(item);  
 
-                   if (obj == null)  
 
-                   {  
 
-                       continue;  
 
-                   }  
 
-                   
 
-                   
 
-                   if (isStoreDB && proInfo.PropertyType == typeof(DateTime) && Convert.ToDateTime(obj) < Convert.ToDateTime("1753-01-01"))  
 
-                   {  
 
-                       continue;  
 
-                   }  
 
-                   
 
-                   dr[proInfo.Name] = obj;  
 
-                   
 
-               }  
 
-               dt.Rows.Add(dr);  
 
-           }  
 
-           return dt;  
 
-       }  
 
 
上面判断时间范围原因与table转集合一样。
3、提取DataTable某一行转为指定对象
 
-    
 
-    
 
-    
 
-    
 
-    
 
-    
 
-    public static T TableToEntity<T>(DataTable dt, int rowindex = 0, bool isStoreDB = true)  
 
-    {  
 
-        Type type = typeof(T);  
 
-        T entity = Activator.CreateInstance<T>(); 
 
-        if (dt == null)  
 
-        {  
 
-            return entity;  
 
-        }  
 
-        
 
-        
 
-        DataRow row = dt.Rows[rowindex]; 
 
-        PropertyInfo[] pArray = type.GetProperties();  
 
-        foreach (PropertyInfo p in pArray)  
 
-        {  
 
-            if (!dt.Columns.Contains(p.Name) || row[p.Name] == null || row[p.Name] == DBNull.Value)  
 
-            {  
 
-                continue;  
 
-            }  
 
-   
 
-            if (isStoreDB && p.PropertyType == typeof(DateTime) && Convert.ToDateTime(row[p.Name]) < Convert.ToDateTime("1753-01-02"))  
 
-            {  
 
-                continue;  
 
-            }  
 
-            try  
 
-            {  
 
-                var obj = Convert.ChangeType(row[p.Name], p.PropertyType);
 
-                p.SetValue(entity, obj, null);  
 
-            }  
 
-            catch (Exception)  
 
-            {  
 
-                
 
-            }  
 
-            
 
-        }  
 
-        
 
-        return entity;  
 
-    }  
 
 
以上三个方法有些代码注释掉,但是没有删除,是给大家一个参考,代码中有啥不对或者需要优化的地方还请大家批评指出,我会尽快改正,谢谢!
C#.net开发 List与DataTable相互转换 【转】
原文:http://www.cnblogs.com/mazhenyu/p/7156830.html