打开一个SqlCommand时使用的参数为一个DataTable [英] Turning a SqlCommand with parameters into a DataTable

查看:145
本文介绍了打开一个SqlCommand时使用的参数为一个DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我适应了一些code是别人写的,并需要返回一个DataTable时间的缘故。

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

我有code是这样的:

I have code like this:

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;
}

这篇关于打开一个SqlCommand时使用的参数为一个DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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