sla*_*dau 5 .net c# datatable sqldatareader sql-server-2008
string query = "select * from cfo_daily_trans_hist";
try
{
using (SqlConnection connection = new SqlConnection(
cnnString))
{
SqlCommand command = new SqlCommand(query);
command.Connection = connection;
connection.Open();
var result = command.ExecuteReader();
DataTable datatable = new DataTable();
datatable.Load(result);
connection.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,var result
通过创建ExecuteReader();
和HasRows
是true
,它显示领域的正确的金额.但是,DataTable
我从它创建的是空的.
我究竟做错了什么?我99%肯定它正在获取数据,但我不知道如何通过该SqlDataReader
对象找到它以确保.
谢谢.
小智 6
而不是a SqlDataReader
,使用a SqlDataAdapter
.
SqlDataAdapter myAdapter = new SqlDataAdapter(command);
myAdapter.Fill(datatable);
Run Code Online (Sandbox Code Playgroud)
使用a SqlDataAdapter
,您不需要显式调用SqlConnection.Open()
和SqlConnection.Close()
.它在该Fill()
方法中处理.
归档时间: |
|
查看次数: |
11869 次 |
最近记录: |