1. Execute datatable.select ("conditions") in the DataTable to return to the DataTable;
<summary>
Executing a query in a DataTable returns a new DataTable
</summary>
DT is the source data DataTable
Condition is a query condition
DataTable NEWDT = new DataTable ();
NEWDT = dt. Clone (); Cloning DT structure, including all DT architectures and constraints, and no data;
datarow[] rows = dt. Select (conditions); Search for qualified records from DT;
foreach (DataRow row in rows)//Adds the results of the query to the DT;
{
Newdt. Rows.Add (row. ItemArray);
}
Some netizens said can also be like this: (Everyone can try)
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 out all
Select (filter condition);//Filter by filter conditions, such as select ("Columnname1 like '%xx% '");
Select (Filter conditions, sort fields);//filter, and sort, such as select ("Columnname1 like '%xx% '", columnname2);
After completing a query that returns a DataTable, many times you want to continue searching in the query results. The results can then be queried using the DataTable.Select method.
The Select method has 4 overloads, and what we often use is datatable.select (String);
Let's talk about DataTable.Select (String) with one parameter:
The parameter of this string is the qualifier of the query. Corresponds to the WHERE statement (without where) in the SQL query language, whose syntax conforms to the SQL language syntax. I think it is a SQL-like syntax.
But I tried, not supporting between and, for example, a success:
FromTime and ToTime are two variables of the datetime type; 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 string inside must be a column name and data, and a relational operator such as >,<,=,<>. For a few examples:
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 mark problem; I used to put the variable in double quotation marks, has been wrong, and later on the Internet, found to have double quotation marks, and then use single quotation marks;
Execute DataTable.Select ("condition") < go > in C # datatable