重用数据源

时间:2010-04-05 06:01:29

标签: c# .net asp.net

我想要使用一个数据库调用并将该数据重用于其他控件 - 而无需再进行其他调用。场景:我调用books表来返回所有作者和标题。我创建了一个名为list1的作者列表控件,以显示莎士比亚的所有标题和列表2,以显示查尔斯狄更斯的标题。

Void Bindme()
{

string commandText = "Select * from books";

        SqlCommand mycommand = new SqlCommand(commandText, datasource1);
        datasource1.Open();
        SqlDataReader myReader1 = mycommand.ExecuteReader();


        list1.DataSource = myReader1;
        list1.DataBind();

        list2.DataSource = myReader1;
        list2.DataBind();

        datasource1.Close();
}

在我的示例中,只有第一次绑定到源list1才能获取数据。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你可以这样做。就像下面一样。



string commandText = "Select * from books where firstcondition;Select * from books where secondcondition";

        SqlCommand mycommand = new SqlCommand(commandText, datasource1);
        datasource1.Open();
        SqlDataReader myReader1 = mycommand.ExecuteReader();

 list1.DataSource = myReader1;
 list1.DataBind();

 myReader1.NextResultSet();

 list2.DataSource = myReader1;
 list2.DataBind();