欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2021-03-20 10:00:05|栏目:.NET代码|点击:

复制代码 代码如下:

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;
        }

上一篇:C#实现基于ffmpeg加虹软的人脸识别的示例

栏    目:.NET代码

下一篇:.net实现文件读写的几种常用方法

本文标题:C#读取数据库返回泛型集合详解(DataSetToList)

本文地址:http://www.codeinn.net/misctech/84179.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有