cdu*_*dub 42 .net c# datatable sqlcommand .net-3.5
我正在调整其他人编写的一些代码,并且需要为了时间而返回DataTable.
我有这样的代码:
using (SqlCommand command = new SqlCommand(query, conn))
{
//add parameters and their values
using (SqlDataReader dr = command.ExecuteReader())
{
return dr;
}
Run Code Online (Sandbox Code Playgroud)
但是返回数据表的最佳方法是什么?
Tho*_*ena 79
使用DataTable.Load方法使用SqlDataReader中的值填充表:
using (SqlDataReader dr = command.ExecuteReader())
{
var tb = new DataTable();
tb.Load(dr);
return tb;
}
Run Code Online (Sandbox Code Playgroud)
小智 11
通过使用DBDataAdapter
ms文档摘录
// Create the DbDataAdapter.
DbDataAdapter adapter = new DbDataAdapter();
adapter.SelectCommand = command;
// Fill the DataTable.
DataTable table = new DataTable();
adapter.Fill(table);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
53063 次 |
最近记录: |