合併兩個DataTable

2022-05-20 21:57:53 字數 1828 閱讀 2902

方法1:

sqlconnection myconnection

=new

sqlconnection(

"server=localhost;uid=sa;pwd=;database=wjoa");

string

sql1="

select * from resoursemanage_t";

sqldataadapter adaptera

=new

sqldataadapter(sql1,myconnection);

dataset ds

=new

dataset();

adaptera.fill(ds,"a

");string

sql2="

select * from userlogin_t";

if(myconnection.state 

!=connectionstate.closed)

myconnection.close();

adaptera

=new

sqldataadapter(sql2, myconnection);

adaptera.fill(ds,"b

");datatable dtparent

=ds.tables["a

"];datatable dtchild

=ds.tables["b

"];datarow dr;

for(

inti=0

;i<

dtchild.rows.count;i++)

dtparent.rows.add(dr);

}this

.datagrid1.datasource

=ds.tables["a

"].defaultview;

this

.datagrid1.databind();

方法2:

//兩個結構一樣的dt合併

datatable datatable1 = new datatable();

datatable datatable2 = new datatable();

datatable newdatatable = datatable1.clone();

object obj = new object[newdatatable.columns.count];

for (int i = 0; i < datatable1.rows.count; i++)

for (int i = 0; i < datatable2.rows.count; i++)

//兩個結構不同的dt合併

///

/// 將兩個列不同的datatable合併成乙個新的datatable

///

/// 表1

/// 表2

/// 合併後新的表名

///

private datatable unitedatatable( datatable dt1 ,datatable dt2 ,string dtname)

object obj = new object[dt3.columns.count];

for (int i = 0; i < dt1.rows.count; i++)

if( dt1.rows.count >= dt2.rows.count )}}

else

for( int i = 0 ;i < dt2.rows.count ;i++ )}}

dt3.tablename = dtname ; //設定dt的名字

return dt3 ;

}

如何合併兩個結構相同的DataTable

今天遇到了乙個情況,就是從一張資料表中讀取幾個符合條件1的客戶的資訊,然後再讀取幾個符合條件2的客戶的資訊,最後顯示出來.因為前後兩次資料的客戶資訊的結構是完全相同的,所以乾脆合併成乙個datatable再賦值給gridview好了.寫了個函式.合併兩個相同的datatable,返回合併後的結果 p...

連線兩個DataTable表

在c 中把兩個datatable連線起來,相當於sql的inner join方法 public static datatable join datatable first,datatable second,datacolumn fjc,datacolumn sjc datacolumn parent...

合併兩個byte

byte sshead system.text.encoding.unicode.getbytes this is head byte sscontent system.text.encoding.unicode.getbytes this is content.sshead sscontent b...