C reads the database and returns the generic collection of DataSetToList

  • 2020-06-01 10:56:57
  • OfStack


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>         
        ///  Gets the generic collection          
        /// /// </summary>         
        /// /// <typeparam name="T"> type </typeparam>         
        /// /// <param name="connStr"> Database connection string </param>         
        /// <param name="sqlStr"> To query the 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"> Conversion type </typeparam>         
        /// <param name="dataSet"> The data source </param>         
        /// <param name="tableIndex"> The index of the table needs to be transformed </param>        
        /// /// <returns> Generic collections </returns>
        public IList<T> DataSetToList<T>(DataSet dataset,int tableIndex)
        {
            // Verify that the parameters are valid 
            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++)
            {
                // Create a generic object 
                T _t=Activator.CreateInstance<T>();
                // Gets all the properties of the object 
                PropertyInfo [] propertyInfo=_t.GetType().GetProperties();
                // Properties and names are assigned at the same time 
                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;
        }


Related articles: