Useful methods for dataset and datatable

Source: Internet
Author: User
Dataset and datatable can be used to export data to: http://www.cnblogs.com/pauline/archive/2011/04/11/2012593.html# :paine

Each dataset is a collection of one or more able objects (datatable is equivalent to a table in the database). These objects are composed of datarow, datacolumn, and column name) item, and the relationship (Relations) related to the data in the datatable object and the data display sorting (dataview) information.

Dataview is used to provide sorting and filtering functions when observing data. Datacolumn is used to limit the data values in a table. For example, what is the default value of a column of data, What is the range of the column's data value, which is the primary key, and whether the data value is read-only.

Since one dataset may have multiple tables and these tables may have associations, parentrelations and childrelations are used for representation. The parentrelations table is a parent table, the childrelations table is a child table, and the child table is a reference to the parent table, so that a row in a table is associated with a row in another table or even the entire table.

1. datatablecollection class

Indicates the set of dataset tables.

Datatablecollection DTC = Ds. tables;

Datatable customertable = DTC ["product"];

2. datatable Dynamic Filtering and sorting

Call datatable. Select () to obtain the array of the datarow object.

(1) obtain all rows.

Datarow [] rows = DT. Select ();

(2) obtain the rows that meet the filtering conditions in the primary key order (if there is no primary key, the rows are added in the order.

Datarow [] rows = DT. Select ("ID> 52 ");

(3) obtain the rows that meet the filtering conditions and sort them by the specified sorting conditions.

Datarow [] rows = DT. Select ("ID> 52", "id DESC ");

(4) obtain the rows that meet the filtering conditions and the specified status, and sort the rows by the specified sorting conditions.

String strexpr = "id> 52 ";

String strsort = "id DESC ";

Datarow [] foundrows = DT. Select (strexpr, strsort, dataviewrowstate. originalrows );

3. datatable for data statistics

When using SQL Server, we can easily perform sum, aver, count, and other operations on the data to obtain statistical results. Then, after the dataset (datatable) that has been retrieved from the data) how to make statistics? In particular, the dataset is obtained through a third-party Interface Web service. At this time, there is no way to execute the SELECT statement to obtain these statistics. What should I do?

There are always more than the problem. In fact, data statistics can also be performed in the datatable.

The following uses a few simple examples to describe how to easily obtain the record statistical results in the datatable without having to calculate records one by one. Compute is a powerful datatable function.

(1) Function Description:

Public object compute (string strexpression, string strfilter );

L strexpression: expression string to be calculated, basically similar to the statistical expression in SQL Server.

L strfilter: A Statistical filter string. Only records that meet this filter condition are counted.

(2) Call example.

Assume that a product sales table p_sale describes the sales records of each salesperson in the mall, as shown in Table 5-2.

Table 5-2 Product Sales

Serial number

Column name

Data Type

Length

Primary Key

Null allowed

Description

1

ID

Int

4

√

 

Serial number

2

Name

Varchar

50

 

√

Name

3

Sex

Smallint

2

 

√

Gender: 0 is female, 1 is male

4

Birthday

Datetime

8

 

√

Birthday

5

Productid

Varchar

20

 

√

Sales product code

6

Num

Int

4

 

√

Sales quantity

7

Price

Decimal

9

 

√

Sales Price

L count the number of salespersons whose gender is female:

Object n = table. Compute ("count (ID)", "Sex = 0 ");

L count the total number of salespersons older than 20:

Int c = (INT) Table. Compute ("count (ID )",

"Birthday <'" datetime. Today. addyears (-20 )"'");

L average prices of products sold:

Decimal ap = (decimal) Table. Compute ("AVG (price)", "true ");

L count the number of products sold with the product code 1:

Object M = table. Compute ("sum (Num)", "productid = 'sj '");

L total sales of all products: to calculate the total sales amount, table does not have the sales amount of a certain promoter of a product, but we can get it through quantity * price. For example, table. Compute ("sum (quantity * price)", "true ");.

One problem here is that datatable's statistical function is not powerful than sqlserver, and this statement is incorrect! Because compute statistics do not support data such as sum (quantity * price. What should we do?

For Statistics of such complex data, we can create a new datacolumn in the datatable, for example, "Total", and set the expression of this field to quantity * price, in this way, we can use the statistical function.

Datacolumn Dc = new datacolumn ("Total", type. GetType ("system. Decimal "));

DC. Expression = "Num * price ";

Table. Columns. Add (DC );

Object S = table. Compute ("sum (total)", "true ");

Of course, this function can also be used to add a template column in The DataGrid to implement computation in the itemdatabind event.

4. Merge the data of two able tables

Datatable dt1 = Ds. Tables [0];

Datatable dt2 = Ds. Tables [1];

Dt1.merge (dt2, true, missingschemaaction. addwithkey );

5. dataview class

(1) Obtain dataview.

Dataview DV = Ds. Tables [0]. defaultview;

// Or

Dataview DV = new dataview (Ds. Tables ["product"], "id> 52", "id DESC ",

Dataviewrowstate. currentrows );

(2) obtain the row data of dataview.

Foreach (datarowview rowview in DV)

{

For (INT I = 0; I <DV. Table. Columns. Count; I)

{

Response. Write (rowview [I] "<br> ");

}

}

(3) filter and sort the result set.

Dataview DV = Ds. Tables [0]. defaultview;

DV. rowfilter = "id> 52 ";

DV. Sort = "id DESC ";

Int c = DV. count;

If (C> 51)

{

For (INT n = 50; n <C; N)

{

DV. Delete (N );

}

}

This. Maid = DV;

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.