我试了试,好像DataReader只能在B/S下绑定到DataGrid,而C/S模式下不可使用。如下代码:
private SqlDataReader reader;
private void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
DataBinds();
}
}
private void DataBinds()
{
reader=CreateDataSource();
this.DataGrid1.DataSource=reader;
this.DataGird1.DataBind();
reader.Close();
}
private SqlDataReader CreateDataSource()
{
SqlConnection con=new SqlConnection("database=Northwind;server=localhost;uid=sa;pwd=sa");
SqlCommand cmd=new SqlCommand("select * from Products",con);
con.Open();
SqlDataReader readers=cmd.ExecuteReader(CommandBehavior.CloseConnection);
return readers;
}