开发者

c#/sql-server, trying to get data to two ddl's from two tables...in one sqlDataReder

Well, i am tring to do something nice (nice for me, simple for you guys), i was told i can do it, but i have no idea where to start. I have two DDL's in a page, i need on page_load to popolate both, each one gets data from deferent table with 开发者_开发百科no relaition between them (suppliers/catagories). i know how to do it with two DB connections, that is easy, but i was told that i can do it with one connection. I was not told if it is only the connection that is united or also that SP deal with both tables in one SP (doesn't seem logical to me that i can do it with only one SP...but what do i know..lol) thanks, Erez


You can run both the queries in the SP:

your_sp

  select * from table1;
  select * from table2;

Then on C# side, you open the data reader and you can use the reader.NextResult() method to move to the next result in the result set.

while (reader.Read())                                              
{                                                                  
   // process results from first query                             
}                                                                  

reader.NextResult();                                               
while (reader.Read())                                              
{                                                                  
   // process results from second query                            
}                                                                  


I think you could separate your SQL statements by a semicolon.
e.g. SELECT myColumns FROM Suppliers; SELECT otherColumns FROM Categories

You could open the datareader in a regular way.

Once you are done reading all the data for 1st resultset, you could make a call to NextResult which will try to execute the next statement and will get you the reader for 2nd resultset.

Note: I have not done this. But this is what I can make out of the documentation.


Not exactly the way I'd do it (i'd use 2 object data sources); but if you really only want to use 1, do this:

Create one sql statement that contains 2 select statements; and load that into a C# DataSet.
Bind your first ddl to DataSet.Tables[0].
Bind your second ddl to DataSet.Tables[1].

There ya go. 1 connection.

EDIT: If you really want to use a DataReader...

You'd probably need 2 SELECT statements, with an additional field to distinguish which DDL you're inserting into. So; something like this:

SELECT 'Table1' AS TableName, Name, Value FROM dbo.Table1

UNION

SELECT 'Table2' AS TableName, Name, Value FROM dbo.Table2

and then in whatever method you're using to load items into your DDL's, check the table name to see which one to add it into

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜