"Return multiple result sets" and "MARS" instances

Source: Internet
Author: User

Multiple result sets are returned:

A simple query can return multiple result sets. Returning multiple result sets in a single query can improve the query efficiency and avoid occupying multiple database tutorial links at the same time.

 

  
/// <Summary>
/// Return multiple result sets
/// </Summary>
/// <Returns> </returns>
Public void gettworesultset ()
{
Sqlconnection conn = new sqlconnection (_ connstr );
String SQL = "select * from table_1; select * from table_2 ";
Sqlcommand com = new sqlcommand (SQL, conn );
Using (conn)
{
Conn. open ();
Sqldatareader dr = com.exe cutereader ();
While (dr. read ())
{
//... The binding Code is omitted.
}

// Read the data to the next result set (the focus is here)
Dr. nextresult ();

While (dr. read ())
{
//... The binding Code is omitted.
}
}
}
2. Use mars:
Ado.net provides a new feature named mars. Before this version, database connections can only represent one query result set within a limited period of time. If you use the mars feature, you can use a single database connection to represent multiple query result sets.
By default, the mars feature is disabled. To enable this feature, you only need to add a field attribute in the database connection string: multipleactiveresultsets = true

 

  
/// <Summary>
/// Use mars (multiple active resultsets, multi-activity result set)
/// </Summary>
Public void getbymars ()
{
// Multipleactiveresultsets = true; enables the SQL mars
String connstr = @ "multipleactiveresultsets = true; datasource =. sqlexpress; integrated security = true ;";
Connstr + = @ "attachdbfilename = | datadirectory | mydatabase. mdf; user instance = true ";
Sqlconnection conn = new sqlconnection (connstr );

String sqlcata = "select * from catagory ";
Sqlcommand extends Cata = new sqlcommand (sqlcata, conn );

String SQL = "select * from movies where catagoryid = @ catagoryid ";
Sqlcommand cmd = new sqlcommand (SQL, conn );
Cmd. parameters. add ("@ catagoryid", sqldbtype.int );

Using (conn)
{
Conn. open ();
Sqldatareader drcata = cmdcata.exe cutereader ();
While (drcata. read ())
{
Int cataid = int. parse (drcata ["catagoryid"]. tostring ());

Cmd. parameters ["@ catagoryid"]. value = cataid;
Sqldatareader dr = cmd.exe cutereader ();
While (dr. read ())
{
//... The binding Code is omitted.
}
Dr. close ();
}
}
}

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.