DataSet和DataReader的不同用法

来源:互联网 发布:unity3d gui text 编辑:程序博客网 时间:2024/05/17 03:28

发现有些人不知道DataSet和DataReader的不同用法,
喜欢直接把DataSource赋值为一个Reader,然后就DataBind()。
出不来数据再在CSDN上发贴说我全部地方都没有错啊,为什么不出数据。。。

以一个下拉框DropDownList1绑定为例子:
现在一个DataSet(DS)和一个DataReader(DR)里是相同的数据。
用DS的话是:
DropDownList1.DataSource=DS.Tables[0];
DropDownList1.......//Text和Value分别绑定字段。
DropDownList1.DataBind();
用DR的话是:
while(DR.Read())
{DropDownList1.ItemsAdd(new ListItem(DR["字段名1"].ToString(),DR["字段名2"].ToString()));}