Fill the datatable with datareader

Source: Internet
Author: User

 I. Introduction

Binding repeater to datareader is a common and efficient form of data binding. Sometimes you can use the data source control (datasource) of ASP. net2.0, but it does not feel flexible enough.
In general, repeater and datareader can be directly bound:

Sqldatareader Dr = New Sqlutility. executereader (...);
Repeater. datasource = Dr;
Repeater. databind ();

However, sometimes, when repeater data is bound for secondary classification, dropdownlist must also be bound to the same data source, and datareader must directly read the data again, if the data read by datareader is saved to the able, the data can be bound n times.

II. Implementation

1. Fill the datatable with datareader directly
CodeAs follows:

// Piao: http://www.cnblogs.com/zxjay   Public Datatable datareadertodatatable (idatareader reader)
{
Datatable TB =   New Datatable ();
Datacolumn Col;
Datarow row;
Int I;

For (I =   0 ; I < Reader. fieldcount; I ++ )
{
Col =   New Datacolumn ();
Col. columnname = Reader. getname (I );
Col. datatype = Reader. getfieldtype (I );
TB. Columns. Add (COL );
}

While (Reader. Read ())
{
Row = TB. newrow ();
For (I =   0 ; I < Reader. fieldcount; I ++ )
{
Row [I] = Reader [I];
}
TB. Rows. Add (ROW );
}
Return TB;
}

2. asp. net2.0 has a simpler method:

Datatable dt = New Datatable ();
DT. Load (idatareader );

One statement.
For more information about datatable. Load (...), see the open-source Mono Project (http://www.go-mono.com /).
File Location: (mono-1.2.4 \ MCS \ class \ system. Data \ system. Data \ datatable. CS)

3. Summary:

No pai_^

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.