C#读取数据库返回泛型集合(DataSetToList)

ExpandedBlockStart.gif 代码
  protected   void  Page_Load( object  sender, EventArgs e)
        {
            
if  ( ! IsPostBack)
            {
                IList
< LYZX.Model.LYZX_NewsTypeModel >  list  =  GetList < LYZX.Model.LYZX_NewsTypeModel > (System.Configuration.ConfigurationManager.ConnectionStrings[ " ConnStr " ].ConnectionString,
               
" select * from LYZX_NewsType " );

                GridView1.DataSource 
=  list;
                GridView1.DataBind();
            }
        }

        
public   string  GetNewsTypeLink( ref   string  baseUrl,Guid newsType)
        {
            
return   "" ;
        }

        
///   <summary>          
        
///  获取泛型集合         
        
///   ///   </summary>          
        
///   ///   <typeparam name="T"> 类型 </typeparam>          
        
///   ///   <param name="connStr"> 数据库连接字符串 </param>          
        
///   <param name="sqlStr"> 要查询的T-SQL </param>          
        
///   <returns></returns>          
         public  IList < T >  GetList < T > ( string  connStr,  string  sqlStr)        
        {             
            
using  (SqlConnection conn  =   new  SqlConnection(connStr))            
            {                
                
using  (SqlDataAdapter sda  =   new  SqlDataAdapter(sqlStr, conn))                
                {                  
                    DataSet ds 
=   new  DataSet();                     
                    sda.Fill(ds);                    
                    
return  DataSetToList < T > (ds,  0 );                
                }            
            }       
        }


        
///   <summary>          
        
///  DataSetToList         
        
///   </summary>           
        
///   <typeparam name="T"> 转换类型 </typeparam>          
        
///   <param name="dataSet"> 数据源 </param>          
        
///   <param name="tableIndex"> 需要转换表的索引 </param>         
        
///   ///   <returns> 泛型集合 </returns>
         public  IList < T >  DataSetToList < T > (DataSet dataset, int  tableIndex)
        {
            
// 确认参数有效
             if  (dataset == null   ||  dataset.Tables.Count <= 0 ||  tableIndex < 0 )
            {
                
return   null ;
            }

            DataTable dt 
=  dataset.Tables[tableIndex];

            IList
< T >  list  =   new  List < T > ();


            
for  ( int  i  =   0 ; i  <  dt.Rows.Count; i ++ )
            {
                
// 创建泛型对象
                T _t = Activator.CreateInstance < T > ();

                
// 获取对象所有属性
                PropertyInfo [] propertyInfo = _t.GetType().GetProperties();

                
// 属性和名称相同时则赋值
                 for  ( int  j  =   0 ; j  <  dt.Columns.Count; j ++ )
                {
                    
foreach  (PropertyInfo info  in  propertyInfo)
                    {
                        
if  (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper()))
                        {
                            
if  (dt.Rows[i][j] != DBNull.Value)
                            {
                                info.SetValue(_t, dt.Rows[i][j], 
null );
                            }
                            
else
                            {
                                info.SetValue(_t, 
null null );
                            }

                            
break ;
                        } 
                    }
                }

                list.Add(_t);
            }

            
return  list;
        }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值