将具有参数的SqlCommand转换为DataTable [英] Turning a SqlCommand with parameters into a DataTable
本文介绍了将具有参数的SqlCommand转换为DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码:
$ b我正在调整一些别人写的代码,需要返回一个DataTable$ b
using(SqlCommand command = new SqlCommand(query,conn))
{
//添加参数及其值
using(SqlDataReader dr = command.ExecuteReader())
{
return dr;
}
但是返回数据表的最佳方式是什么?
解决方案
使用 DataTable.Load 方法用SqlDataReader中的值填充表:
using(SqlDataReader dr = command.ExecuteReader())
{
var tb = new DataTable();
tb.Load(dr);
返回tb;
}
I'm adapting some code that someone else wrote and need to return a DataTable for time's sake.
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?
解决方案
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屋!
查看全文