Explanation of common ADO. Net objects: DataSet objects

Source: Internet
Author: User

The function of dataset in ADO. NET is to provide a disconnected storage for the data source without having to worry about the data source. You can only perform operations in dataset.
Important objects in Dataset:
Tablescollection object:The tables in dataset are represented by datatable. a dataset can contain multiple datatable tables, which constitute the tablescollection object. Each able contains a columnscolleciton and a rowscollection object.
Relationscollection object:The relationships between datarelation tables are expressed by datarelation. The collection composed of these datarelations is the relationscollection object.
Extendedproperties object:This object is used to define specific information, such as the password and update time.

1. datatable object
Create a datatable:

Create a able
Datatable mytable;
Mytable =   New Datatable ( " Test " );
Mytable. casesensitive = False; // Case Sensitive
Mytable. minimumcapacity =   100 ; // Minimum database record Space

Create a table column

Create a table column
Datatable mytable;
Datacolumn mycolumn;

Mytable =   New Datatable ( " Table Name " );

Mycolumn = Mytable. Columns. Add ( " Column name " , Typeof ( String ));
Mycolumn = Mytable. Columns. Add ( " Column name " , Typeof ( Int ));

Create expression Column

Example
// Method 1
Datacolumn tax =   New Datacolumn ();
Tax. datatype =   Typeof (Currency );
Tax. Expression =   " Total * rate X 0.20 " ;

// Method 2
Mytable. Columns. Add ( " Tax " , Typeof (Currency ), " Total * rate X 0.20 " );

2. dataview object
Dataview provides an external mode for the database structure. At the same time, dataview can also provide the data binding function for Form Controls and Web controls. A dataview is built in each able to be datatable. defaultview ().
Create dataview:
Dataview sortedview = new dataview (datatable );
Sort dataview:
Datatable. defaultview. Sort = "lastname ";
Datatable. defaultview. Sort = "lastname, firstname DESC ";
Filter and sort dataview:

Use rowfilter attribute settings to filter data
Dataview dv =   New Dataview (Ds. Tables [ " Authors " ]);
DV. rowfilter =   " State = 'CA' " ;
DV. Sort =   " Au_lname " ;

3. datacolumn object Example

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> datacolumn colcustomerid = dtcustomers. columns. add ( " customerid " , typeof (int32 ));
colcustomerid. allowdbnull = false ;
colcustomerid. unique = true ;

4. datarow object
Call the newrow method to create a new datarow object. Create a datarow object
Datarow drnewemployee = Dtemployees. newrow ();

// Use indexes or column names to operate new rows
Drnewemployee ( 0 ) =   11 ;
Drnewemployee ( 1 ) =   " Smith " ;

// Call the add method to Add rows to datarowcollection.
Dtemployees. Rows. Add (drnewemployee );

Batch modify rows:
Beginedit () starts to change, endedit () ends the change, and writes the change result to dataset, canceledit (), cancel the change
For example:
Row. beginedit ();
Modify row
Row. endedit ();

Delete A datarow object from datatable:
I. Remove Method of datarowcollection object

Example
Datarow dremployee=Dtemployees. Rows (3);
Dtemployees. Rows. Remove (dremployee );

Ii. Delete method of datarow objectExample
Dremployee. Delete;

Comparison: When the remove method is used, datarow is deleted from datarowcollection, while the dalete method only marks the deleted row.
The datarow class includes the rowstate attribute. The rowstate attribute value indicates whether the row is changed, how it is changed, and how it is changed, starting from the first creation of the able (or loading the datatable from the database. Optional attribute value: Modified | detached | added.

5. Create a table relationship Example
// Create datarelation
Datarelation Dr;
Datacolumn parentcol;
Datacolumn childcol;

Parentcol = DS. Tables [ " MERs " ]. Columns [ " Customerid " ];
Childcol = DS. Tables [ " Orders " ]. Columns .[ " Customerid " ];
Dr =   New Datarelation ( " Custorders " , Parentcol, childcol );
DS. relations. Add (DR );

Currentparentrow = DS. Tables [ " MERs " ]. Rows [maid. selectedindex];
Foreach (Datarow R In Currentparentrow. getchildrow ( " Custorders " ))
{
Lable1.text+ =R ["Orderid"]+ ",";
}

6. Bind dataExample 1
Gridview. datasource=DS;
Gridview. datamember= "Authors";
Gridview. databind (); Example 2
Gridview. datasource=DS. Tables ["Authors"];
Gridview. databind (); Example 3

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> dataview DV = New dataview (Ds. tables [ " authors " ]);
DV. rowfilter = " state = 'CA' " ;< br> gridview. datasource = DV;
gridview. databind ();

postscript ~
after writing for one night, my hands were sour. After reading the book on n sides, I did understand that there was a level above, but there were still many complicated things I could not understand! However, the basic content should be enough!

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.