zoukankan      html  css  js  c++  java
  • C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合

    /// <summary>
            /// 获取UserInfo泛型集合
            /// </summary>
            /// <param name="connStr">数据库连接字符串</param>
            /// <param name="sqlStr">要查询的T-SQL</param>
            /// <returns></returns>
            public IList<UserInfo> GetUserInfoAll(string connStr, string sqlStr)
            {
                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    using (SqlCommand cmd = new SqlCommand(sqlStr, conn))
                    {
                        SqlDataReader sdr = cmd.ExecuteReader();
    
                        IList<UserInfo> list = new List<UserInfo>();
    
                        while (sdr.Read())
                        {
    
                            UserInfo userInfo = new UserInfo();
    
                            userInfo.ID = (Guid)sdr["ID"];
    
                            userInfo.LoginName = sdr["LoginName"].ToString();
    
                            userInfo.LoginPwd = sdr["LoginPwd"].ToString();
    
                            list.Add(userInfo);
    
                        }
                        return list;
                    }
                }
            }
    
            /// <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;
            }

        public class UserInfo
        {
            public System.Guid ID { get; set; }

            public string LoginName { get; set; }

            public string LoginPwd { get; set; }
        }

  • 相关阅读:
    laravel5.2 开发中打印sql语句
    centos 安装 vsftpd
    linux 安装 DenyHosts 防止密码被暴力破解
    linux nginx 安装防火墙ngx_lua_waf
    mysql 下载资源地址
    微信公众号 access_token 没有过期 却失效
    centos 安装 composer
    五十个小技巧提高PHP执行效率
    yii 使用DB实现rbac 权限控制
    git 的使用
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/2471733.html
Copyright © 2011-2022 走看看