我适应了一些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;
}