Cooperation between sqlcommand of ADO. NET 2.0 and sqldataadapter

Source: Internet
Author: User

(This post is posted on the old blog of the version engineer on)
 
Sometimes, according to the project requirements, we may need to obtain multiple sets of datatables from the database, and thenLineCompile the code and present it on the ASP. NET 2.0 page in the desired format, instead of directly binding it to the gridview control. You can refer to the following practices.
 
To join data from multiple tables in the database, multiple records (quantity is not fixed) and multiple columns are retrieved and displayed in ASP in a custom format. NET page, we can first get the data, temporarily put in the memory of multiple able (a datatableArray), And then display it according to the desired style. For more information, see the post 「Use the nextresult method of ADO. Net to obtain multiple result setsThe sqlcommand is used to obtain the join data from multiple tables in the database, assign the sqlcommand to a sqldataadapter, and then use the fill method of sqldataadapter, enter the data from multiple tables into multiple data tables in the memory (one data tableArray); In ADO. NET 2.0, datatable no longer needs to be attached to dataset. Key PointsCodeAs follows:

 

Sqlconnection Conn= New Sqlconnection (Database Link string in Web. config );
Sqldataadapter da= New Sqldataadapter ();
Sqlcommand cmd= New Sqlcommand ();
Cmd. Connection= Conn;

Cmd. commandtext= "Enter the first group of SQL statements, such as select and join. ;" ;
Cmd. commandtext+ = "Enter the second set of SQL statements, such as select and join." ;

Da. selectcommand= CMD;

Datatable [] dtarray =   New Datatable [] {NewDatatable ("Table Name of the first SQL statement"),NewDatatable ("Table Name of the second SQL statement")} ;

//Fill (startrecord, maxrecords, datatable array name)
Da. Fill (0,0 , Dtarray );

Conn. open ();

//Display the content of the first able (dtarray [0]) on the control of the page
For(IntI= 0; I<Dtarray [0]. Rows. Count; I++ )
{
//Here, the content of the first able is displayed on the control of the page in a custom format.
}

//You can also directly assign the datatable to the gridview control.
Gridview1.datasource=Dtarray [0 ]. Defaultview;

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.