DataReader读取返回list

本文详细阐述了使用C#语言结合SQL语句进行数据库数据查询,并通过自定义方法将查询结果转换为特定数据结构的过程。

    public ActionResult Index()
        {
            string ConStr = ConfigurationManager.AppSettings["constr"];
            string sql = "select * from UserInfo";

            SqlConnection conn = new SqlConnection(ConStr);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            // IList<UserInfo> list = ToList<UserInfo>(reader);
            IList<UserInfo> list = new List<UserInfo>();
            //获取UserInfo的类型
            Type type = typeof(UserInfo);
            if (type.IsValueType || type == typeof(string))
            {
                //如果是值类型
                while (reader.Read())
                {
                    UserInfo local = (UserInfo)Convert.ChangeType(reader[0], type, null);
                    list.Add(local);
                }
            }
            else
            {
                //获取UserInfo的公共属性
                PropertyInfo[] properties = type.GetProperties();
                string name = string.Empty;
                while (reader.Read())
                {
                    //创建UserInfo实例
                    UserInfo local = Activator.CreateInstance<UserInfo>();
                    //如果reader的列名和属性名相等,则把它的值赋给该属性
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        name = reader.GetName(i);
                        foreach (PropertyInfo info in properties)
                        {
                            if (name.Equals(info.Name))
                            {
                                info.SetValue(local, Convert.ChangeType(reader[info
                                    .Name], info.PropertyType), null);
                                break;
                            }
                        }
                    }
                    //添加到list集合
                    list.Add(local);
                }
            }
            conn.Close();
            return View(list);
        }

        private IList<T> ToList<T>(SqlDataReader reader)
        {
            Type type = typeof(T);
            IList<T> list = null;
            if (type.IsValueType || type == typeof(string))
                list = CreateValue<T>(reader, type);
            else
                list = CreateObject<T>(reader, type);
            reader.Dispose();
            reader.Close();
            return list;
        }

        private IList<T> CreateObject<T>(SqlDataReader reader, Type type)
        {
            IList<T> list = new List<T>();
            PropertyInfo[] properties = type.GetProperties();
            string name = string.Empty;
            while (reader.Read())
            {
                T local = Activator.CreateInstance<T>();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    name = reader.GetName(i);
                    foreach (PropertyInfo info in properties)
                    {
                        if (name.Equals(info.Name)) { info.SetValue(local, Convert.ChangeType(reader[info.Name], info.PropertyType), null); break; }
                    }
                }
                list.Add(local);
            }
            return list;
        }

        private IList<T> CreateValue<T>(SqlDataReader reader, Type type)
        {
            IList<T> list = new List<T>();
            while (reader.Read())
            {
                T local = (T)Convert.ChangeType(reader[0], type, null);
                list.Add(local);
            }
            return list;
        }

    }

转载于:https://www.cnblogs.com/tanzhen/p/4494103.html

个人曾经写过的数据访问接口,包含:MSSQL、Mysql、Oracle等数据库的公共处理接口。可以拿过来直接使用,放在手里好多年了。 IDatabase接口声明如下: namespace Simple.Database { /// /// IDatabase 接口 /// public interface IDatabase { DbConnection dbConn { get; set; } /// /// 创建 DbConnection 对象实例。 /// /// DbConnection 对象实例。 DbConnection CreateConnection(); /// /// 创建 DbCommand 对象实例。 /// /// DbCommand 对象实例。 DbCommand CreateCommand(); /// /// 创建 DbCommand 对象实例。 /// /// Sql 语句或存储过程名。 /// CommandType 参数。 /// DbCommand 对象实例。 DbCommand CreateCommand(string text, CommandType type); /// /// 创建 DbCommand 对象实例。 /// /// Sql 语句或存储过程名。 /// CommandType 参数。 /// 参数集合。 /// DbCommand 对象实例。 DbCommand CreateCommand(string text, CommandType type, IDataParameter[] paras); /// /// 创建 DbCommand 对象实例。 /// /// DbConnection 对象。 /// Sql 语句或存储过程名。 /// CommandType 参数。 /// 参数集合。 /// DbCommand 对象实例。 DbCommand CreateCommand(DbConnection conn, string text, CommandType type, IDataParameter[] paras); /// /// 创建 DbDataAdapter 对象实例。 /// /// DbDataAdapter 对象实例。 DbDataAdapter CreateDataAdapter(); /// /// 创建 DbParameter 对象实例。 /// /// DbParameter 对象实例。 DbParameter CreateParameter(); /// /// 创建 DbParameter 对象实例。 /// /// 参数名称。 /// 参数值。 /// DbParameter 对象实例。 DbParameter CreateParameter(string name, Object value); /// /// 创建 DbParameter 对象实例。 /// /// 参数名称。 /// 参数类型。 /// DbParameter 对象实例。 DbParameter CreateParameter(string name, DbType type); /// /// 创建 DbParameter 对象实例。 /// /// 参数名称。 /// 参数类型。 /// 数据的最大大小。 /// DbParameter 对象实例。 DbParameter CreateParameter(string name, DbType type, int size); /// /// 获取指定长度数据的 DataSet 对象。 /// /// 要读取的 Sql 语句。 /// 开始读取位置的索引。 /// 待读取记录集的长度。 /// DataSet 对象。 DataSet GetDataSet(string sql, int start, int length); /// /// 获取指定长度数据的 DataTable 对象。 /// /// 要读取的 Sql 语句。 /// 开始读取位置的索引。 /// 待读取记录集的长度。 /// DataTable 对象。 DataTable GetDataTable(string sql, int start, int length); /// /// 执行Insert、Update、Delete等操作,并返回受影响的记录数。 /// /// 要执行的 Sql 语句。 /// 受影响的记录数。 int GetEffect(string sql); /// /// 执行 Insert、Update、Delete 等操作,并返回受影响的记录数。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 的类型,即该命令是 Sql 语句,还是存储过程名等。 /// 受影响的记录数。 int GetEffect(string sql, CommandType type); /// /// 执行带参数的 Sql 语句或存储过程,并返回受影响的记录数。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数类型,即该命令是 sql 语句,还是存储过程名等。 /// 参数集合。 /// 受影响的记录数。 int GetEffect(string text, CommandType type, IDataParameter[] paras); /// /// /// /// /// /// /// /// /// int GetEffect(DbConnection conn, string text, CommandType type, IDataParameter[] paras, DbTransaction DbTrans); /// /// /// /// /// List ExecuteTransaction(params string[] sqls); /// /// 执行 Select 语句,并返回 DataSet 对象。 /// /// 要执行的 Sql 语句。 /// DataSet 对象。 DataSet GetDataSet(string sql); /// /// 执行 Select 语句或存储过程,并返回 DataSet 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数类型,即该命令是 sql 语句,还是存储过程名等。 /// DataSet 对象。 DataSet GetDataSet(string text, CommandType type); /// /// 执行带参数的 Sql 语句或存储过程,并返回 DataSet 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数类型,即该命令是 sql 语句,还是存储过程名等。 /// 参数集合。 /// DataSet 对象。 DataSet GetDataSet(string text, CommandType type, IDataParameter[] paras); /// /// 执行 Select 语句,并返回 DataTable 对象。 /// /// 要执行的 Sql 语句。 /// DataTable 对象。 DataTable GetDataTable(string sql); /// /// 执行 Select 语句或存储过程,并返回 DataTable 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数类型,即该命令是 sql 语句,还是存储过程名等。 /// DataTable 对象。 DataTable GetDataTable(string text, CommandType type); /// /// 执行带参数的 Sql 语句或存储过程,并返回 DataTable 对象。 /// /// 要执行的 Sql 语句或存储过程名等。 /// CommandType 参数类型,即该命令是 sql 语句,还是存储过程名等。 /// 参数集合。 /// DataTable 对象。 DataTable GetDataTable(string text, CommandType type, IDataParameter[] paras); /// /// 获取查询所返回的结果集中第一行第一列的值。 /// /// 要处理的 sql 语句(包含待查询的字段)。 /// 字段值。 object GetField(string sql); /// /// 获取查询所返回的结果集中第一行指定列的值。 /// /// 待查询的数据表名称。 /// 待获取字段的列名。 /// 字段值。 object GetField(string sql, string field); /// /// 获取查询所返回的结果集中第一行指定列集合的值。 /// /// 要处理的 sql 语句。 /// 待获取字段的列表。 /// 字段值集合。 object[] GetField(string sql, params string[] fields); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值