帮帮忙,一个关于SqlDataReader返回值的问题?
SqlDataReader是读取行的只进流方式,一次只能读取一条数据,只有在SqlDataReader.Read()为True时才会继续读取下一条记录,可是为什么用DataGrid和SqlDataReader来显示数据时,却可以一次性读取全部数据呢,比如下面的代码:
<% @page language= "c# " Debug= "true " %>
<% @import Namespace= "System.Data " %>
<% @import Namespace= "System.Data.SqlClient " %>
<asp:datagrid id= "datagrid1 " align= "center " runat= "server "/>
<script language= "c# " runat= "server ">
void page_load(object sender,EventArgs e){
SqlConnection con1;
con1=new SqlConnection( "server=localhost;database=hua;uid=sa;pwd= ' ' ");
SqlCommand cmd1;
cmd1=new SqlCommand( "select * from grade ",con1);
con1.Open();
SqlDataReader dr;
dr=cmd1.ExecuteReader();
datagrid1.DataSource=dr;
datagrid1.DataBind();
con1.Close();
}
</script>
------解决方案--------------------这样是全部
你要用dr.read()控制