用 DataTable 替换 DataReader [英] Replacing a DataReader with a DataTable

查看:29
本文介绍了用 DataTable 替换 DataReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在改编一些其他人编写的代码,为了时间的缘故需要返回一个 DataTable.

I'm adapting some code that someone else wrote and need to return a DataTable for time's sake.

我有这样的代码:

using (SqlCommand command = new SqlCommand(query, conn))
{
      //add parameters and their values

      using (SqlDataReader dr = command.ExecuteReader())
      {
          return dr;
      }

但是返回数据表的最佳方式是什么?

But what's the best way to return a datatable?

推荐答案

使用 DataTable.Load 方法用来自 SqlDataReader 的值填充您的表:

Use the DataTable.Load method to fill your table with values from the SqlDataReader:

using (SqlDataReader dr = command.ExecuteReader())
{
    var tb = new DataTable();
    tb.Load(dr);
    return tb;
}

这篇关于用 DataTable 替换 DataReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆