Clear Asp.net (10): dataadapter/dataset of ADO. net

Source: Internet
Author: User

I. Dataset

Dataset is the core of non-connection data access. Is a data container.

Dataset contains two important elements:

1. Zero or multiple relation sets

2. Set of zero or multiple tables (able)

Datatable can reduce the interactions with the database by searching for specific rows.

Find a specific row from datatable:

1 datarow [] matchrows = Ds. Table ["Products"]. Select ("class = 34 ")

Each datatable contains the following elements:

1. Row set

2. sublink set

3. column set

4. Parent relationship set

5. column set

6. defaultview

Data sources such as gridview can be dataset, able, and dataview.

When using dataview as the data source, you can use dataview. sort and dataview. rowfilter to set conditional Filtering for sorting (see the following example ).

Ii. dataadapter

Dataadapter is a bridge between data sources and dataset.

The main method of dataadapter:

1. Fill ()

2. fillschema ()

Execute the query in selectcommand, but only obtain the schema information. Use column names, data types, primary keys, and constraints to pre-configure datatable.

3. Update ()

Check the changes in the datatable, and execute the appropriate insertcommand, updatecommand, and deletecommand operations to Perform Batch update for the data source.

 

3. process from database to Dataset

1. The connection object establishes a connection with the database through the connection string.

2. Set the connection, commandtext, and commandtype attributes of the command object.

3. Set attributes of dataadapter, such as selectcommand, insertcommand, updatecommand, and deletecommand.

4. Call the fill method of the dataadapter object to populate the query data executed by selectcommand of dataadapter into dataset.

Call the update method of the dataadapter object to update the data.


4. Example of joint use of dataadapter and Dataset

C # 1 protected void bindgridview ()
2 {
3 string connstr = system. configuration. configurationmanager. connectionstrings ["mydemo"]. connectionstring;
4 sqlconnection conn = new sqlconnection (connstr );
5 sqlcommand COM = new sqlcommand ("select * from productions", Conn );
6
7 sqldataadapter da = new sqldataadapter (COM );
8 dataset DS = new dataset ();
9 da. Fill (DS, "productions ");
10
11 datacolumn computedcolumn = new datacolumn ("computedcolumn", typeof (INT), "productionid + categoryid ");
12 Ds. Tables ["productions"]. Columns. Add (computedcolumn );
13
14 dataview dv1 = Ds. Tables ["productions"]. defaultview;
15
16 dataview dv2 = new dataview (Ds. Tables ["productions"]);
17 dv2.sort = "categoryid DESC, productionname ASC ";
18
19 dataview dv3 = new dataview (Ds. Tables ["productions"]);
20 dv3.rowfilter = "categoryid> 1 and Len (productionname)> 5 ";
21
22 This. gridview1.datasource = dv1;
23 This. gridview2.datasource = dv2;
24 This. gridview3.datasource = dv3;
25
26 This. databind ();
27}

 

Clear Asp.net series learning blog directory 

 

Reference: Pro ASP. NET 3.5 in C #2008

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.