2010-04-05 25 views
1

我正试图使用​​一个数据库调用并将其用于其他控件的数据重用,而无需再次调用。情景:我打电话给书桌,它返回所有作者和标题。我创建了一个名为list1的作者列表控件,以显示莎士比亚的所有标题和列表2,以显示Charles Dickens的标题。重用数据源

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的第一个绑定获取数据。有任何想法吗?

+0

<插入(可能是坏的)建议使用这里的ORM> – Earlz 2010-04-05 06:03:30

回答

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(); 

 
+0

更妙的是,OP会使用两个数据集,因为他不需要重复使用,现在他实际上是在数据库过滤** **,而不是他的节目。更清晰地阅读,更少混淆。 – ANeves 2010-04-05 10:33:28