也許有人和我一樣有這樣的需求,所以分享給有需要的人。
0
1
public
static
void AppendDataTable(DataTable hostDt, DataTable clientDt)
0 2
{
03
if (hostDt != null && hostDt.Rows.Count > 0)
04
{
05
DataRow dr;
06![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
07 for (int i = 0; i < clientDt.Columns.Count; i++)
08
{
09
hostDt.Columns.Add(new DataColumn(clientDt.Columns[i].ColumnName));
10![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
11 if (clientDt.Rows.Count > 0)
12
for (int j = 0; j < clientDt.Rows.Count; j++)
13
{
14
dr = hostDt.Rows[j];
15
dr[hostDt.Columns.Count - 1] = clientDt.Rows[j][i];
16
dr = null;
17
}
18
}
19
}
20
}
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/None.gif)
0 2
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedBlockStart.gif)
03
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
04
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockStart.gif)
05
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
06
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
07 for (int i = 0; i < clientDt.Columns.Count; i++)
08
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockStart.gif)
09
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
10
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
11 if (clientDt.Rows.Count > 0)
12
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
13
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockStart.gif)
14
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
15
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
16
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
17
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockEnd.gif)
18
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockEnd.gif)
19
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockEnd.gif)
20
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedBlockEnd.gif)
之所以沒有回傳是因為Reference Type Object原本就是passed by reference。然而,如果你的來源資料表(例如這邊的hostDt和clientDt)在呼叫過後會被disposed,那此方法就要改成如下:
0
1
public
static DataTable AppendDataTable(DataTable hostDt, DataTable clientDt)
0 2
{
03
if (hostDt != null && hostDt.Rows.Count > 0)
04
{
05
DataRow dr;
06![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
07 for (int i = 0; i < clientDt.Columns.Count; i++)
08
{
09
hostDt.Columns.Add(new DataColumn(clientDt.Columns[i].ColumnName));
10![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
11 if (clientDt.Rows.Count > 0)
12
for (int j = 0; j < clientDt.Rows.Count; j++)
13
{
14
dr = hostDt.Rows[j];
15
dr[hostDt.Columns.Count - 1] = clientDt.Rows[j][i];
16
dr = null;
17
}
18
}
19
}
20![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
21 return hostDt.Copy();
22
}
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/None.gif)
0 2
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedBlockStart.gif)
03
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
04
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockStart.gif)
05
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
06
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
07 for (int i = 0; i < clientDt.Columns.Count; i++)
08
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockStart.gif)
09
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
10
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
11 if (clientDt.Rows.Count > 0)
12
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
13
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockStart.gif)
14
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
15
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
16
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
17
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockEnd.gif)
18
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockEnd.gif)
19
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockEnd.gif)
20
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/InBlock.gif)
21 return hostDt.Copy();
22
![[C]合併兩個DataTable (更新) - tuchengju - tuchengju的博客](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedBlockEnd.gif)
第1行的宣告以及第21行的回傳是有變動的地方。
6/26/2009更新:
hostDT就是要被附加上去的目的資料表,而clientDT是要附加上去的來源資料表。如果你的hostDT長得像這樣:
ID | Name |
1 | kennyshu |
2 | edith |
3 | alice |
而clientDT是這樣:
Zip | State |
70802 | LA |
11373 | NY |
合併之後會變成這樣:
ID | Name | Zip | State |
1 | kennyshu | 70802 | LA |
2 | edith | 11373 | NY |
3 | alice |
要注意的是,來源資料表的列數(row)不可以比目的資料表來得多,否則會丟出例外訊息。