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