C# - 将 DataReader 转换为 DataTable

C# - Converting DataReader to DataTable

我想将 DataReader 转换为 DataTable 以显示数据库中列表 (demoClients) 中的所有客户。

目前我有这个:

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using MySqlConnector;
    using ProjectDatabase.Controllers;
    using System.Data;
    using System.Data.Common;
    namespace ProjectDatabase.Models
    {
        public class demoClientsQuery
        {
            public AppDb Db { get; }
    
            public demoClientsQuery(AppDb db)
            {
                Db = db;
            }
    
            public async Task<demoClients> FindAllClientsAsync(int id)
            {
                using var cmd = Db.conDemo.CreateCommand();
                cmd.CommandText = @"Query";
                cmd.Parameters.Add(new MySqlParameter
                {
                    ParameterName = "@id_customer",
                    DbType = DbType.Int32,
                    Value = id,
                });
                
                MySqlDataReader dataReader = cmd.ExecuteReader();
                DataTable dataTable = new DataTable();
                dataTable.Load(dataReader);
                var result = await ReadAllAsync(dataTable);            
                return result.Count > 0 ? result[0] : null;
            }
    
    
            public async Task<List<demoClients>> LatestClientsAsync()
            {
                using var cmd = Db.conDemo.CreateCommand();
                cmd.CommandText = @"Query";
    
                var aa = await LatestClientsAsync2();
                
                MySqlDataReader dataReader = cmd.ExecuteReader();
                DataTable dataTable = new DataTable();
                dataTable.Load(dataReader);            
                return await ReadAllAsync(dataTable);
            }
    
            public async Task<DataTable> LatestClientsAsync2()
            {
                using var cmd = Db.conDemo.CreateCommand();
                cmd.CommandText = @"Query";
    
    
                //return await ReadAllAsync(await cmd.ExecuteReaderAsync());
                return await ToDataTable(cmd);
            }
    
            private async Task<DataTable> ToDataTable(MySqlCommand cmd)
            {
    
    
                cmd.CommandType = CommandType.Text;
                using (DbDataAdapter dataAdapter = new MySqlDataAdapter(cmd))
                {
                    cmd.CommandType = CommandType.Text;                               
                    DataTable data = new DataTable();                
                    dataAdapter.Fill(data);
                    
                    return data;
                }
               
            }
    
            private async Task<List<demoClients>> ReadAllAsync(DataTable dataTable)
            {
                var adb_demo_clients = new List<demoClients>();
                using (dataTable)
                {
                    
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        int id_customer = Convert.ToInt32(dr["id_customer"]);
                        string delivery_person_name = Convert.ToString(dr["delivery_person_name"]);
                        string firstname = Convert.ToString(dr["firstname"]);
                        string lastname = Convert.ToString(dr["lastname"]);
                        string email = Convert.ToString(dr["email"]);
                    }
                    
                }
                return adb_demo_clients;
            }
       
        }
    }

我遇到的唯一问题是它 return 什么都没有,它 return 是空的,我的代码中的某些内容没有 return 从我的数据库中获取我的数据.

如有任何帮助,我们将不胜感激。

感谢您的宝贵时间。

您必须将项目添加到 adb_demo_clients 列表中,在 foreach 内,使用 ReadAllAsync 方法:

private async Task<List<demoClients>> ReadAllAsync(DataTable dataTable)
{
    var adb_demo_clients = new List<demoClients>();
    using (dataTable)
    {       
        foreach (DataRow dr in dataTable.Rows)
        {
            demoClients d = new demoClients();

            d.id_customer = Convert.ToInt32(dr["id_customer"]);
            d.delivery_person_name = Convert.ToString(dr["delivery_person_name"]);

            // all fields that you need
            ...
            
            // Add the item in your List
            adb_demo_clients.add(d);
        }
        
    }
    return adb_demo_clients;
}