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