1. Execute datatable. Select ("condition") in datatable to return datatable;
// <Summary>
// Execute the query in the datatable to return the new datatable
/// </Summary>
// DT is the source data datatable
// Condition is the query Condition
Datatable newdt = new datatable ();
Newdt = DT. Clone (); // clone the DT structure, including all DT architectures and constraints without data;
Datarow [] rows = DT. Select (conditions); // query matching records from DT;
Foreach (datarow row in rows) // Add the query result to DT;
{
Newdt. Rows. Add (row. itemarray );
}
Some netizens said they could do the same: (you can try it)
Datatable newdt = new datatable ();
Newdt = DT. Clone ();
Datarow [] DR = DT. Select (condition );
For (INT I = 0; I <dr. length; I ++)
{
Newdt. importrow (datarow) Dr [I]);
}
2. About datatable. Select ();
Select method:
Select (); // check all
Select (filter condition); // filter by filtering condition, such as select ("columnname1 like '% XX % '");
Select (filter condition, sorting field); // filter and sort, such as select ("columnname1 like '% XX %'", columnname2 );
After a query is completed and a datatable is returned, you often want to continue searching in the query results. In this case, you can use the datatable. Select method to re-query the results.
The select method has four overload values. We often use datatable. Select (string );
The following describes the datatable. Select (string) with a parameter ):
The string parameter is a query limitation. It is equivalent to the where Statement (excluding where) in the SQL query language. Its Syntax conforms to the SQL language syntax. I think it is similar to the SQL syntax.
However, I tried it and it does not support between and. For example:
// Fromtime and totime are two datetime-type variables; occurtime is the column name in dtable;
Datarow [] datarows = dtable. Select ("occurtime> = '" + fromtime + "' and occurtime <= '" + totime + "'");
The datatable. Select () method supports simple filtering and sorting, and does not support complex conditional filtering and sorting. The strings must be column names and data, and Relational operators such as >,<,=, <>. For example:
Datarow [] ROW = detailtb. select ("wzmc = '" + materialname + "' and cz = '" + materialtexture + "and GG ='" + materialspecs + "'");
Datatable. Select ("city like 'B % '");
Datatable. Select ("name = '" + A + "'");
Be sure to pay attention to the single quotation marks. I used to enclose the variables in double quotation marks and keep making mistakes. Later I checked the variables online and found that double quotation marks should be used in single quotation marks; that is, 'variable "';