Execute DataTable.Select ("condition") < go > in C # datatable

Source: Internet
Author: User

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

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.