Two new methods for loading data from datatable in ado.net 2.0

Source: Internet
Author: User
Vs2005 has been released for a while. Recently, we have seen Article More and more, I couldn't help but install vs2005.

In framwork1.1, datatable generally uses two methods to load data: one from dataset and the other from custom datatable to fill data with datareader, in framwork2.0, some methods are added in addition to these two methods. For example, the abled readxml Method for loading data from XML. Here I will refer to dataadapter. fill Method and datatabled load method.

Used hereCodePOST method writingProgramDrag the gridview control on the page. The background code is as follows:
Example 1:

// Use the fill method to fill the datatable
Private   Void Usedatatablebyfill ()
{
Sqlconnection myconnection =   New Sqlconnection (configurationmanager. connectionstrings [ " Sqlconnectionstring " ]. Connectionstring );
Datatable mydatatable =   New Datatable ();

Sqldataadapter mydp =   New Sqldataadapter ( " Select * from authors " , Myconnection );
Mydp. Fill (mydatatable );

Gridview1.datasource = Mydatatable. defaultview;
Gridview1.databind ();

Myconnection. Dispose ();
Mydp. Dispose ();

}

Example 2: // Use the datareader Method
Private   Void Usedatatablebydatareader ()
{
Sqlconnection myconnection =   New Sqlconnection (configurationmanager. connectionstrings [ " Sqlconnectionstring " ]. Connectionstring );
Datatable mydatatable =   New Datatable ();

Sqlcommand mycommand =   New Sqlcommand ( " Select * from authors " , Myconnection );
Myconnection. open ();

Sqldatareader Dr = Mycommand. executereader (commandbehavior. closeconnection );
Mydatatable. Load (DR );

Gridview1.datasource = Mydatatable. defaultview;
Gridview1.databind ();

Dr. Close ();
Dr. Dispose ();
Mycommand. Dispose ();
}

You can call these two events to display data.
After testing, we found that the two methods show that the data cannot use the pagination and sorting events that come with the gridview, and the features such as the other events of the gridview and the calculation column of the Abel are not tested.

Related Article

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.