In the following example, 3 Join methods are implemented to connect two DataTable, equivalent to the SQL inner join method, and return all columns of the DataTable.
If the DataColumn in the two DataTable is duplicated, the second is set to Columnname+ "_second", and the following code is in the hope of helping.
Using System;
Using System.Data;
Namespace WindowsApplication1
{
public class Sqlops
{
Public Sqlops ()
{
}
public static DataTable Join (DataTable A, DataTable Second, datacolumn[] FJC, datacolumn[] SJC)
{
Create a new DataTable
DataTable table = new DataTable ("Join");
Use a DataSet to leverage DataRelation
Using (DataSet ds = new DataSet ())
{
Copy the DataTable into the dataset
Ds. Tables.addrange (New Datatable[]{first.copy (), Second.copy ()});
datacolumn[] Parentcolumns = new DATACOLUMN[FJC. Length];
for (int i = 0; i < Parentcolumns. Length; i++)
{
Parentcolumns[i] = ds. Tables[0]. Columns[fjc[i]. ColumnName];
}
datacolumn[] Childcolumns = new DATACOLUMN[SJC. Length];
for (int i = 0; i < Childcolumns. Length; i++)
{
Childcolumns[i] = ds. TABLES[1]. Columns[sjc[i]. ColumnName];
}
Create Association
DataRelation r = new DataRelation (string. Empty,parentcolumns,childcolumns,false);
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.