DataSet dsSource = new DataSet(); //來源DataSet
DataTable dtSource = new DataTable(); //來源DataSet

//複製DataSet
DataSet dsTarget = dsSource.Copy();  //複製格式與資料

DataSet dsTarget = dsSource.Clone(); //只複製格式

//複製DataTable
DataTable dtTarget = dtSource.Copy(); //複製格式與資料
DataTable dtTarget = dtSource.Clone(); //只複製格式

//複製DataRow
1 . ImportDataRow
方法:public void ImportDataRow( DataRow DataRow);
DataTable dtTarget = dtSource.clone();//必須先複製DataTable的格式

foreach (DataRow dr in dtSource)
    dtTarget.ImportDataRow(dr);
//dtTarget中添加一個新行,並將dr的值複製進去,要求資料表的格式一樣!


自定義複製
dtTarget.Columns.Add ("id");
//不需要有一樣的格式,只複製自己需要的欄位!
 
Object [] myArry = new Object
[1]; 
foreach (DataRow
dr in dtSource)

   
DataRow drNew = dtTarget.NewRow();//必須呼叫此方法!

    myArry[0] = dr["id"];
//如果myArry中沒有來源資料表中的id列的話就會產生錯誤!
    drNew.ItemArray = myArry;
//ItemArray屬性為Object類型數組,根據程式的需要可以自行複製多個欄位的資料
!
    dtTarget.Rows.Add(drNew); //必須呼叫此方法!,否則DataRow中的數據將不能顯示!

}


3  LoadDataRow
方法:public DataRow LoadDataRow(Object[] values,bool fAcceptChanges);
Object[] newRow = new Object
[3];
//設定資料

newRow[0] = "Hello";
newRow[1] = "World";
newRow[2] = "two";
DataRow myRow;
dtTarget.BeginLoadData();
// 將新資料列添加到資料表中

myRow = dtTarget.LoadDataRow(newRow,
true);//標誌要設置為true,表示新增資料列

dtTarget.EndLoadData();

arrow
arrow
    全站熱搜

    souts 發表在 痞客邦 留言(0) 人氣()