C# - 将 DataReader 转换为 DataTable
Posted
技术标签:
【中文标题】C# - 将 DataReader 转换为 DataTable【英文标题】:C# - Converting DataReader to DataTable 【发布时间】:2021-06-23 15:24:30 【问题描述】:我想将 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;
我遇到的唯一问题是它什么都不返回,它返回空,我的代码中的某些东西没有从我的数据库中返回我的数据。
任何帮助将不胜感激。
感谢您的宝贵时间。
【问题讨论】:
doesn't return nothing
- 所以它确实返回了一些东西
您的MySqlConnection
和您的connectionString
在哪里?你能连接到数据库吗?或者你在数据表中转换你的回报有问题吗?
直到DataTable
派生自IDisposable
,您可以将其放入using
语句中
您知道您的方法ReadAllAsync
只读取数据表行但从不向列表中添加任何内容adb_demo_clients
?
foreach (DataRow dr in dataTable.Rows) demoClients d = new demoClients(); d.Id = Convert.ToInt32(dr["id_customer"]);; //fill all you fields that you need
所以在那之后你必须在你的列表中添加adb_demo_clients.add(d);
(在你关闭你的foreach
之前)
【参考方案1】:
您必须在 foreach
内的 ReadAllAsync
方法上将项目添加到 adb_demo_clients
列表中:
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;
【讨论】:
以上是关于C# - 将 DataReader 转换为 DataTable的主要内容,如果未能解决你的问题,请参考以下文章
C# - 无法将类型“IBM.Data.DB2.iSeries.iDB2DataReader”隐式转换为“System.Data.SqlClient.SqlDataReader”
如何轻松将 DataReader 转换为 List<T>? [复制]