国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#讀取數(shù)據(jù)庫(kù)返回泛型集合詳解(DataSetToList)

2020-01-24 02:53:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

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">數(shù)據(jù)庫(kù)連接字符串</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">轉(zhuǎn)換類型</typeparam>        
        /// <param name="dataSet">數(shù)據(jù)源</param>        
        /// <param name="tableIndex">需要轉(zhuǎn)換表的索引</param>       
        /// /// <returns>泛型集合</returns>
        public IList<T> DataSetToList<T>(DataSet dataset,int tableIndex)
        {
            //確認(rèn)參數(shù)有效
            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++)
            {
                //創(chuàng)建泛型對(duì)象
                T _t=Activator.CreateInstance<T>();

                //獲取對(duì)象所有屬性
                PropertyInfo [] propertyInfo=_t.GetType().GetProperties();

                //屬性和名稱相同時(shí)則賦值
                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;
        }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 渑池县| 海南省| 扶余县| 平安县| 石河子市| 句容市| 分宜县| 万源市| 绥德县| 广安市| 台州市| 饶阳县| 吴堡县| 益阳市| 石首市| 新兴县| 怀仁县| 金沙县| 梁平县| 普洱| 张北县| 安图县| 崇明县| 齐齐哈尔市| 山丹县| 潞城市| 临海市| 法库县| 玛曲县| 神农架林区| 双鸭山市| 广河县| 禹城市| 彩票| 商河县| 六枝特区| 北辰区| 合江县| 平陆县| 乾安县| 建阳市|