Retrieve multiple returned result sets (related to the use of the nextresult method of the datareader object)

Source: Internet
Author: User


You can use the nextresult method of the datareader object to retrieve the next result set. If the return value of this method is false, it indicates that the last result set has been reached. For example:

Public   Void Page_load (Object sender, eventargs E)
{
// Connect to the student database of the Local Computer
String strconn =   " Server = (local); database = test; trusted_connection = Yes " ;
Sqlconnection CN =   New Sqlconnection (strconn );

//Open connection
CN. open ();

// Create a command object, which contains two query statements
String S1 = " Select ID from student; " ;
String S2 = " Select name from student " ;
Sqlcommand cm =   New Sqlcommand (S1 + S2, CN );

//Create a datareader object
Sqldatareader Dr=Cm. executereader ();

// Read the query results of an SQL statement in sequence using the read method of datareader
Do
{
// Read the query results of an SQL statement in sequence using the read method of datareader
While (Dr. Read ())
{
Object [] Cols =   New   Object [ 2 ];
Int N = Dr. getvalues (Cols );
Message. Text = Message. Text +   " / "   + Dr [ 0 ]. Tostring ();
}
// Use the nextresult () method of datareader to read the query results of the next SQL statement.
Message. Text = Message. Text +   " **** " ;
}
While (Dr. nextresult ());
// Disable Dr
Dr. Close ();
// Close connection
CN. Close ();
}

 

The results are as follows:

/2001010348/2001011780/2001296531 */Zhang San/Li Si/Wang 'er ****

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.