在C#中把两个DataTable连接起来,相当于Sql的Inner Join方法

在下面的例子中实现了3个Join方法,其目的是把两个DataTable连接起来,相当于Sql的Inner Join方法,返回DataTable的所有列。
如果两个DataTable中的DataColumn有重复的话,把第二个设置为ColumnName+"_Second",下面是代码,希望对大家有所帮助。
None.gif using  System;
None.gif
using  System.Data;
None.gif
None.gif
namespace  WindowsApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class SQLOps
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public SQLOps()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static DataTable Join (DataTable First, DataTable Second, DataColumn[] FJC, DataColumn[] SJC)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
//创建一个新的DataTable
InBlock.gif

InBlock.gif            DataTable table 
= new DataTable("Join");
InBlock.gif
InBlock.gif
InBlock.gif            
// Use a DataSet to leverage DataRelation
InBlock.gif

InBlock.gif            
using(DataSet ds = new DataSet())
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
//把DataTable Copy到DataSet中
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif                ds.Tables.AddRange(
new DataTable[]dot.gif{First.Copy(),Second.Copy()});
InBlock.gif
InBlock.gif                DataColumn[] parentcolumns 
= new DataColumn[FJC.Length];
InBlock.gif
InBlock.gif                
for(int i = 0; i < parentcolumns.Length; i++)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
InBlock.gif                    parentcolumns[i] 
= ds.Tables[0].Columns[FJC[i].ColumnName];
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                DataColumn[] childcolumns 
= new DataColumn[SJC.Length];
InBlock.gif
InBlock.gif                
for(int i = 0; i < childcolumns.Length; i++)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
InBlock.gif                    childcolumns[i] 
= ds.Tables[1].Columns[SJC[i].ColumnName];
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif
InBlock.gif                
//创建关联
InBlock.gif

InBlock.gif                DataRelation r 
= new DataRelation(string.Empty,parentcolumns,childcolumns,false);
InBlock.gif
InBlock.gif                ds.Relations.Add(r);
InBlock.gif
InBlock.gif
InBlock.gif                
//为关联表创建列
InBlock.gif

InBlock.gif                
for(int i = 0; i < First.Columns.Count; i++)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
InBlock.gif                    table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
for(int i = 0; i < Second.Columns.Count; i++)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
InBlock.gif                    
//看看有没有重复的列,如果有在第二个DataTable的Column的列明后加_Second
InBlock.gif

InBlock.gif                    
if(!table.Columns.Contains(Second.Columns[i].ColumnName))
InBlock.gif
InBlock.gif                        table.Columns.Add(Second.Columns[i].ColumnName, Second.Columns[i].DataType);
InBlock.gif
InBlock.gif                    
else
InBlock.gif
InBlock.gif                        table.Columns.Add(Second.Columns[i].ColumnName 
+ "_Second", Second.Columns[i].DataType);
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif               
InBlock.gif
InBlock.gif                table.BeginLoadData();
InBlock.gif
InBlock.gif                
foreach(DataRow firstrow in ds.Tables[0].Rows)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
InBlock.gif                    
//得到行的数据
InBlock.gif

InBlock.gif                    DataRow[] childrows 
= firstrow.GetChildRows(r);
InBlock.gif
InBlock.gif                    
if(childrows != null && childrows.Length > 0)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif
InBlock.gif                        
object[] parentarray = firstrow.ItemArray; 
InBlock.gif
InBlock.gif                        
foreach(DataRow secondrow in childrows)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif
InBlock.gif                            
object[] secondarray = secondrow.ItemArray;
InBlock.gif
InBlock.gif                            
object[] joinarray = new object[parentarray.Length+secondarray.Length];
InBlock.gif
InBlock.gif                            Array.Copy(parentarray,
0,joinarray,0,parentarray.Length);
InBlock.gif
InBlock.gif                            Array.Copy(secondarray,
0,joinarray,parentarray.Length,secondarray.Length);
InBlock.gif
InBlock.gif                            table.LoadDataRow(joinarray,
true);
InBlock.gif
ExpandedSubBlockEnd.gif                        }

InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                table.EndLoadData();
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif            
return table;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static DataTable Join (DataTable First, DataTable Second, DataColumn FJC, DataColumn SJC)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
return Join(First, Second, new DataColumn[]dot.gif{FJC}new DataColumn[]dot.gif{SJC});
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static DataTable Join (DataTable First, DataTable Second, string FJC, string SJC)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
return Join(First, Second, new DataColumn[]dot.gif{First.Columns[FJC]}new DataColumn[]dot.gif{First.Columns[SJC]});
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值