或者
/// 執(zhí)行DataTable中的查詢返回新的DataTable/// </summary>/// <param name="dt">源數(shù)據(jù)DataTable</param>/// <param name="condition">查詢條件</param>/// <returns></returns>PRivate DataTable GetNewDataTable(DataTable dt, string condition,string sortstr){DataTable newdt = new DataTable();newdt = dt.Clone();DataRow[] dr = dt.Select(condition,sortstr);for (int i = 0; i < dr.Length; i++){newdt.ImportRow((DataRow)dr[i]);}return newdt;//返回的查詢結(jié)果
}
或者 逐列的形式
public static int TableDataExchange(DataSet ds, string tableName, DataTable sourceDT) { for(int i=0;i<sourceDT.Rows.Count;i++) { DataRow drNew = ds.Tables[tableName].NewRow(); foreach (DataColumn dc in sourceDT.Columns) { drNew[dc.ColumnName] = sourceDT.Rows[i][dc.ColumnName]; } ds.Tables[tableName].Rows.Add(drNew); } ds.Tables[tableName].AcceptChanges(); DataTable dt = ds.Tables[tableName]; return ds.Tables[tableName].Rows.Count; }
或者
DataSet 對(duì)象是支持 ADO.NET的斷開式、分布式數(shù)據(jù)方案的核心對(duì)象 ,用途非常廣泛.我們很多時(shí)候需要使用其中的數(shù)據(jù),比如取得一個(gè)DataTable的數(shù)據(jù)或者復(fù)制另一個(gè)DataTabe中的數(shù)據(jù)或者是DataRow的數(shù)據(jù),但是只有DataSet和DataTable的復(fù)制是支持深層復(fù)制的,就是說不僅能復(fù)制元素的結(jié)構(gòu),而且能復(fù)制元素的數(shù)據(jù),而DatatDataRow沒有相關(guān)的復(fù)制的方法,下面將簡(jiǎn)單介紹下這些數(shù)據(jù)元素的復(fù)制問題。
DataTable sourceTable;DataTable objectTable;DatatDataRow sourceRow;DatatDataRow objectRow;DataRow tempRow;DataSet souceDataSet = new DataSet();
復(fù)制DataSet
DataSet object = souceDataSet.Copy();//深復(fù)制 DataSet object = souceDataSet.Clone();//淺復(fù)制,只復(fù)制架構(gòu)
復(fù)制DataTable
objectTable = sourceTable .Copy();//深復(fù)制 objectTable = sourceTable .Clone();//淺復(fù)制,只復(fù)制架構(gòu)
復(fù)制DataRow
項(xiàng)目開發(fā)中經(jīng)常遇到這種錯(cuò)誤-“此行已屬于另一個(gè)表” 。導(dǎo)致這個(gè)錯(cuò)誤的語句如下:
objectTable .Rows.Add(SourceDataRow);
分析了一下原因,因?yàn)镈ataRow DataTable 都是傳引用調(diào)用的。所以一個(gè)行在一個(gè)表中了,就不能再增加到另外一個(gè)表。
具體方法:
1 ImportRow方法:public void ImportRow( DataRow DataRow);
objectTable = sourceTable.clone();//必須先復(fù)制表的架構(gòu),使具有相同的的列或關(guān)系!foreach (DataRow oRow in sourceTable){
objectTable.ImportRow(oRow);//在objectTable中添加一個(gè)新行,并將sourceRow的值復(fù)制進(jìn)去,要求表的結(jié)構(gòu)一樣!
}
_____________________________________________________________________________________________________
2. 循環(huán)DataTable的每個(gè)列
DataRow aDataRow = objectTable.NewRow();
foreach(DataColumn aDataColumn in sourceTable.Columns)
{
aDataRow [aDataColumn.ColumnName] = sourceTable[i][aDataColumn.ColumnName];
}
objectTable.Rows.Add(aDataRow);
3. 自定義復(fù)制
objectTable.Columns.Add ("id");//不需要有一樣的架構(gòu),只復(fù)制自己需要的列!Object [] myArry = new Object [1]; foreach (DataRow oRow in sourceTable){
tempRow = objectTable.NewRow();//此方法必須調(diào)用! myArry[0] = oRow["id"];//如果myArry中沒有源表中的id列的話就會(huì)報(bào)錯(cuò)! tempRow.ItemArray = myArry;//ItemArray屬性為Object類型數(shù)組,根據(jù)程序的需要需要可自行復(fù)制多個(gè)列的數(shù)據(jù)! objectTable.Rows.Add(tempRow); //此方法必須調(diào)用,否則DataRow中的數(shù)據(jù)將不能顯示!
}_____________________________________________________________________________________________________
4. LoadDataRow方法:public DataRow LoadDataRow(Object[] values,bool fAcceptChanges);
Object[] newRow = new Object[3]; // 設(shè)置對(duì)象數(shù)組的值 newRow[0] = "Hello"; newRow[1] = "World"; newRow[2] = "two"; DataRow myRow; ObjectTable.BeginLoadData(); // 將新行添加到表中 myRow = ObjectTable.LoadDataRow(newRow, true);//標(biāo)志要設(shè)置為true,表示添加新行 ObjectTable.EndLoadData();
該方法比較復(fù)雜,如果只是簡(jiǎn)單的復(fù)制現(xiàn)有行的數(shù)據(jù)來添加新行的話建議不要采用,具體用法請(qǐng)參看sdk文擋。
或者:
< type="text/javaScrJavascript>
我們經(jīng)常需要向DataTable中添加一行數(shù)據(jù),大多數(shù)的情況下都是把一些從UI的控件和程序的變量中收集的數(shù)據(jù)添加到DataTable中。如以下的語句把供應(yīng)商代碼和名稱添加到DataTable中:
DataTable dtProvider = new DataTable(); DataRow drRow = dtProvider.NewRow(); drRow[0] = txtProviderCode.Text.Trim(); drRow[1] = txtProviderName.Text.Trim(); dtProvider.Rows.Add(drRow);
大多數(shù)的情況下這幾行語句是完全可以滿足要求的。但是如果想把另一個(gè)同樣結(jié)構(gòu)的DataTable的某一行添加到這個(gè)dtProvider中,就不能簡(jiǎn)單的添加了。否則會(huì)提示一個(gè)錯(cuò)誤“This row belongs to another table.”。這個(gè)時(shí)候我們必須定義另一個(gè)DataRow,把源DataRow的數(shù)據(jù)賦到目的DataRow中,再Add進(jìn)DataTable中就可以了。如下所示:
DataTable dtProvider = new DataTable(); DataRow drTarget = dtProvider.NewRow(); drTarget.ItemArry = drSource.ItemArry; // 注意:這里的drSource是另一個(gè)相同結(jié)構(gòu)的DataTable中的一行。 dtProvider.Rows.Add(drTarget); < type="text/JavaScript"> alimama_pid="mm_10249644_1605763_5027492"; alimama_type="f"; alimama_sizecode ="tl_1x5_8"; alimama_fontsize=12; alimama_bordercolor="FFFFFF"; alimama_bgcolor="FFFFFF"; alimama_titlecolor="0000FF"; alimama_underline=0; alimama_height=22; alimama_width=0; < src="http://a.alimama.cn/inf.js" type=text/javascript>
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注