请知道的朋友帮个忙!有关DataRow.Add的问题

ufrshchenw 2005-10-26 10:17:48
我有两个相同的DataSet,想把其中一个DataSet的DataRow加到另一个DataSet中,怎么才能加进去??如果用DataRow.Add(DataRow)的方法,会出 “这个Row已经存在另一个表中” 的错,希望知道的朋友帮帮我!
谢谢大家!
...全文
1123 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
swordragon 2005-10-26
  • 打赏
  • 举报
回复
//假如oldDateSet已经有数据

DataSet newDateSet = new DataSet();

//copy可以拷贝表的结构和全部数据
DataTable newTable = oldDataSet.Tables[0].Copy();
//DataTable newTable = oldDataSet.Tables["表名"].Copy();

newDateSet.Tables.Add(newTable);
thinkingforever 2005-10-26
  • 打赏
  • 举报
回复
Merge方法
ds1.Merge(ds2)就可以把ds2中的所有数据及状态加进ds1中
ds1.Merge(ds2.tables[0])就可以把ds2中的的第一个表及状态加进ds1中
ds1.Merge(DataRows)就可以把DataRows的数据加进ds1,DataRows是筛选出的行的集合
jimh 2005-10-26
  • 打赏
  • 举报
回复
DataRow dr = dt1.Rows[0];
dt1.Rows.Remove(dr);
dt2.Rows.Add(dr);
netpot 2005-10-26
  • 打赏
  • 举报
回复
dsDest.Tables.Rows.Add(dsSource.Tables.Rows[i].ItemArray)
我不懂电脑 2005-10-26
  • 打赏
  • 举报
回复
可以用dataset的合并方法。
private void DemonstrateMergeTableAddSchema(){
// Create a DataSet with one table, two columns, and ten rows.
DataSet ds = new DataSet("myDataSet");
DataTable t = new DataTable("Items");

// Add table to the DataSet
ds.Tables.Add(t);

// Create and add two columns to the DataTable
DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"),"");
c1.AutoIncrement=true;
DataColumn c2 = new DataColumn("Item", Type.GetType("System.Int32"),"");
t.Columns.Add(c1);
t.Columns.Add(c2);

// Set the primary key to the first column.
t.PrimaryKey = new DataColumn[1]{ c1 };

// Add RowChanged event handler for the table.
t.RowChanged+= new DataRowChangeEventHandler(Row_Changed);

// Add ten rows.
for(int i = 0; i <10;i++){
DataRow r=t.NewRow();
r["Item"]= i;
t.Rows.Add(r);
}

// Accept changes.
ds.AcceptChanges();
PrintValues(ds, "Original values");

// Create a second DataTable identical to the first, with
// one extra column using the Clone method.
DataTable t2 = t.Clone();
t2.Columns.Add("extra", typeof(string));

// Add two rows. Note that the id column can't be the
// same as existing rows in the DataSet table.
DataRow newRow;
newRow=t2.NewRow();
newRow["id"]= 12;
newRow["Item"]=555;
newRow["extra"]= "extra Column 1";
t2.Rows.Add(newRow);

newRow=t2.NewRow();
newRow["id"]= 13;
newRow["Item"]=665;
newRow["extra"]= "extra Column 2";
t2.Rows.Add(newRow);

// Merge the table into the DataSet.
Console.WriteLine("Merging");
ds.Merge(t2,false,MissingSchemaAction.Add);
PrintValues(ds, "Merged With Table, Schema Added");
}

private void Row_Changed(object sender, DataRowChangeEventArgs e){
Console.WriteLine("Row Changed " + e.Action.ToString() + "\t" + e.Row.ItemArray[0]);
}

private void PrintValues(DataSet ds, string label){
Console.WriteLine("\n" + label);
foreach(DataTable t in ds.Tables){
Console.WriteLine("TableName: " + t.TableName);
foreach(DataRow r in t.Rows){
foreach(DataColumn c in t.Columns){
Console.Write("\t " + r[c] );
}
Console.WriteLine();
}
}
}

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧