使用ADO.NET连接数据库 DataSet转List集合

日常记录:  因为使用Quartz定时任务组件后导致类中不能成功依赖注入,没法拿到数据,所以我就直接用ADO操作一下;

1、先从配置文件中拿到连接字符串 ,也可以直接写上连接字符;

            IConfiguration configuration = new ConfigurationBuilder()
           .SetBasePath(Environment.CurrentDirectory)
           .AddJsonFile("appsettings.json", true, true) //配置文件名
           .AddInMemoryCollection()
           .Build();
            string sqlConnectionString = configuration["ConnectionStrings:Default"];  //连接字符串名

2、获取数据;

      1.Connection 对象是一个连接对象,主要功能是建立与物理数据库的连接。其主要包括4种访问数据库的对象类,也可称为数据提供程序;

      2.Sql server 数据提供程序, 位于system.Data.SqlClient 命名空间;

      3.Command  对象是一个数据命令对象,主要功能是向数据库发送查询、更新、删除、修改等Sql语句;

      4.DataReader 对象是数据库读取对象,提供只读向前的游标,如果应用程序需要每次每次从数据中取出最新的数据,或者只是需要快速读取数据,并不需要修改,那么就可以使用DataReader 对象进行读取;

      5.DataAdapter 对象是一个数据适配器对象 ,是DataSet 与数据源之间的桥梁。DataAdapter 对象提供了四个属性,用于与数据源之间的互联 SelectCommand 属性 向数据库发送查询语句

      6.DeleteCommand 属性 ,InsertCommand 属性,UpdateCommand属性;

           //连接对象
            SqlConnection con = new SqlConnection(sqlConnectionString);
           //创建一个DataSet
            DataSet LineRe = new DataSet();7
           // 打开连接
            con.Open();
           //SQL语句
            string SqlStr = "select * from LineRelation";
            //数据库命令对象
            SqlCommand command = new SqlCommand(SqlStr, con);
            //数据适配器  
            SqlDataAdapter sda = new SqlDataAdapter();
            //向数据库发送查询SQL语句
            sda.SelectCommand = command;
            //Fill方法用数据填充DataSet
            sda.Fill(LineRe);
            //将转换后的数据放入List集合
            var _lineRelation = new List<LineRelation>();
            //DataSetToList 转换方法
            _lineRelation = DataSetToList<LineRelation>(LineRe, 0).ToList();

3、DataSet转List方法 

        /// <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;
        }
原文地址:https://www.cnblogs.com/LinWenQiang/p/13462270.html