asp.ent merges two DataTable with the same structure

  • 2020-05-07 19:30:14
  • OfStack

I wrote a function.
 
/// <summary> 
///  Merge two identical ones DataTable, Returns the combined result  
/// </summary> 
/// <param name="dt1"></param> 
/// <param name="dt2"></param> 
/// <returns></returns> 
public DataTable CombineTheSameDatatable(DataTable dt1, DataTable dt2) 
{ 
if (dt1.Rows.Count == 0 && dt2.Rows.Count == 0) 
{ 
return new DataTable(); 
} 
if (dt1.Rows.Count == 0) 
{ 
return dt2; 
} 
if (dt2.Rows.Count == 0) 
{ 
return dt1; 
} 
DataSet ds = new DataSet(); 
ds.Tables.Add(dt1.Copy()); 
ds.Merge(dt2.Copy()); 
return ds.Tables[0]; 
} 

Due to my limited understanding of C#, I do not understand why dt1==null cannot correctly judge the empty table, so I use the method of counting lines. I also hope that the superior can give advice to 12.

Related articles: