DataTable sort retrieve merge and other operational instances in C
- 2020-06-19 11:33:41
- OfStack
1. Sort
1. Get the default view of DataTable
2. Set the sort expression for the view
3. Replace DataTable with the new DataTable exported from the sorted view (Asc ascending can be omitted, multi-column sorting separated by ", ")
1) Regeneration method
dstaset.Tables.Add(dt)
dataset.Tables(0).DefaultView.Sort = "id desc"
2) Direct method
dv = New DataView(dt)
dv.Sort = "id desc"
dt = dv.ToTable();
3) Indirect method
dv = New DataView(ds.Tables[0])
dv.Sort = "id desc"
dt = dv.ToTable();
2. Retrieve
Set the query string
Use Select to get an array of all data row objects that meet the criteria (multiple query criteria separated by and)
DataRow[] matches = dt.Select("id<'003' and name=' The name 11'");
string strName = matches[0]["name"].ToString();
3. The merger
Suppose there are two DataTable:Dt1 and Dt2. Table structure 1 sample
This method can be used after attaching Dt2 to Dt1
dt1.Merge(dt2);
4. Query DataTable for issues that should be noted
After completing one query and returning one DataTable, there are many times when you want to continue searching in the query results. You can then re-query the results using the DataTable.Select method
The Select method has four overloads, and the one we often use is DataTable. Select(String)
The argument to String is qualified for the query. It is equivalent to the WHERE statement in SQL query language (excluding WHERE), whose syntax conforms to the SQL language syntax.
The Select method returns DataRow containing the data queried, but this DataRow is only one mapping of the queried DataTable, so DataRow changes with the rows of DataTable. For example, if DataTable's rows are deleted, so is the data in DataRow (even if Select was deleted first, then)
Therefore, in order to put the returned DataRow into the data display control, you need to put it into another DataTable. If you put the original DataTable directly or remove all the rows of the original DataTable before putting the data into the query, it will not work. The program will display an error saying "there is a row in the table".
In addition, inserting rows into a new table cannot be done directly in the DataTable.Rows.Add (DataRow) manner, as this would insert a new table and DataRow would be empty. To import rows, DataTable. ImportRow(DataRow). This assumes, of course, that the new table has the same structure as the original table 1.
Public Function SDEResearch(ByVal InputDT As DataTable, ByVal SearchStr As String) As DataTable
' Used to store the queried tables
Dim ReSearchDT As DataTable = InputDT.Clone() ' Ensure that you have the same table structure as the source data table
' Used to store returns from a query datarow An array of
Dim ReSearchDR() As DataRow = Nothing
Try
ReSearchDR = InputDT.Select("NAME LIKE '%" + SearchStr + "%'") ' It's just mapped out from the table datarow() , so you cannot delete rows from the original table
Catch ex As Exception
Return Nothing
End Try
For i As Int16 = 0 To ReSearchDR.Length - 1
ReSearchDT.ImportRow(ReSearchDR(i))
Next
Return ReSearchDT
End Function
Attachment: Methods for filtering DataTable data
Some methods of filtering DataTable Select,dataview
When you take a little bit of data out of the database and you want to put it together, it's easy to think:
DataTable dt = new DataTable();// Assuming that dt Is made up of "SELECT C1,C2,C3 FROM T1" The result of the query
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["C1"].ToString() == "abc")// Query conditions
{
// operate
}
}
But this kind of practice USES 1 to say twice also, use many tired. Is there a better way? Remember that LinQ can query DataTable directly. Is there a similar method in.Net Framework 2.0? The answer is yes, dt.Select().
DataRow[] drArr = dt.Select("C1='abc'");// The query
You can also do this:
DataRow[] drArr = dt.Select("C1 LIKE 'abc%'");// Fuzzy query
DataRow[] drArr = dt.Select("'abc' LIKE C1 + '%'", "C2 DESC");// On the other 1 A fuzzy query method
DataRow[] drArr = dt.Select("C1='abc'", "C2 DESC");// The sorting
Again, if you want to assign DataRow to the new DataTable, how do you assign it? You might think:
DataTable dtNew = dt.Clone();
for (int i = 0; i < drArr.Length; i++)
{
dtNew.Rows.Add(drArr[i]);
}
But then the program will make a mistake and say that DataRow belongs to another DataTable. So how do you do that? It's very simple, and this solves it:
dv = New DataView(dt)
dv.Sort = "id desc"
dt = dv.ToTable();
0
And we're done.
DataRow[] in 3.5 has an extension of CopyToDataTable()
/*
* supplement 1 Next, you can use it DataView To achieve the purpose of retrieval.
*/
DataTable dataSource = new DataTable();
DataView dv = dataSource.DefaultView;
dv.RowFilter = "columnA = 'abc'";
//1. Directly obtained after filtering DataTable
DataTable newTable1 = dv.ToTable();
//2. Set up the new DataTable the TableName
DataTable newTable2 = dv.ToTable("NewTableName");
//3. Sets whether the new table filters duplicates , The column name of the owned column and the order in which it appears
// That is, you can set the fields for the new table. But the field names are definitely old tables dataSource Owned by.
DataTable newTable3 =
dv.ToTable(true, new string[] { "columnA,columnF,columnC" });
//4. A combination of 2.3 At two o 'clock.
DataTable newTable4 =
dv.ToTable("NewTableName", true, new string[] { "columnA,columnF,columnC" });