Ado. NET Knowledge Point notes

Source: Internet
Author: User

1 DataColumn datatype can be any data type, including self-defined classes and structs.

Datatable dt = new datatable ("Product");             dt. Columns.Add ("ProductId", typeof (int));             Dt. Columns.Add ("Productproperty",  typeof (productproperty));             datarow r1 = dt. NewRow ();             r1["ProductId"] = 1 ;             r1["ProductProperty"] = new  productproperty ()  { ProductionDate = DateTime.Now, Desc =  "Milk sugar"   };            dt. Rows.Add (R1);             foreach (DATAROW DR  in dt. Rows)             {                 console.writeline ("productid {0}; Productiondate:{1};D esc:{2}; ",                     dr["ProductId"]. ToString (),                      (dr["Productproperty"] as productproperty). Productiondate.tostring (),                      (dr["Productproperty"] as productproperty). DESC);             }

2 A DataColumn that has a unique attribute marked true does not allow null values.

3 Determine if a column in the DataRow has been modified

Onerow.datarowstate=datarowstate.changed, otherwise there may be no original version if (Onerow.hasversion (datarowversion.proposed)) {if (     onerow["Salary", datarowversion.original]!=onerow["Salary", datarowversion.proposed]) {//salary field has changed }}

4 using Datarow.beginedit () Datarow.endedit () to delay data validation while reducing the number of rowchanged event firings

Onerow.beginedit (); onerow["ProductName"]= "Colaverygood";//productname MaxLength for 10onerow["UnitPrice"]=12.4;// The UnitPrice maximum value is 10.0onerow.endedit ();//until the data validation exception occurs, and if there is no exception, the RowChanged event occurs without triggering two events.

5 Validation-based errors

With datacolumnchanging,datarowchanging, the data can be modified to verify that data that does not conform to the validator is entered into the column field or the data is not valid.

private void App_columnchanging (Object Sender,datacolumnchangeeventargs e) {if (e.column.columnname== "age") {    if ((int) e.proposedvalue>120) e.row.setcolumnerror (E.column, "Human can ' t live that long"); }   }

6 column-Level errors

public void Showfirstrowerror (DataRow row) {string errorText = "No error!"; datacolumn[] Errorcolumns = row.    Getcolumnsinerror (); if (errorcolumns.count>0) {errortext= row.    Getcolumnerror (Errorcolumns[0]); } else if (row. rowerror.length>0) {errorText = row.            RowError; }//Show ErrorText}

7 Search rows by primary key

DataRow Matchrow = SomeTable.Rows.Find (searchvalue);//single-part keydatarow matchrow = someTable.Rows.Find (New object []{value1,value2}]//multi-part key;//If no row is found, returns null

8 Selecting rows and sorting with search criteria

Match note string case sensitivity//sometable.casesensitive=true;//default case insensitive datarow[] matchrows = Sometable.select (FilterCriteria, Sortrules);//filtercriteria filter Expression Reference datacolumn.expression very powerful//sortrules OrderDate desc,customername ASC,ASC is the default sort, can be omitted.


This article is from the "Smart Barrier World" blog, please make sure to keep this source http://chenwan.blog.51cto.com/2754591/1567286

Ado. NET Knowledge Point notes

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.