我有两个相同的DataSet,想把其中一个DataSet的DataRow加到另一个DataSet中,怎么才能加进去??如果用DataRow.Add(DataRow)的方法,会出 “这个Row已经存在另一个表中” 的错,希望知道的朋友帮帮我!
谢谢大家!
谢谢大家!
解决方案 »
- C#中DataGridview中绑定小数的问题
- 关于C#控件treeView的递归调用问题
- |ZYCWPF| 自定义的RichTextBox,剪切可以复制,但没有切掉内容,如何把菜单写在自定义控件的Generic里面,缩进无效,有完整源码测试 谢谢
- 自定义控件问题,?
- DLL为何 无法生成com interop注册的com文件
- Professional C# - Graphics with GDI+一书的代码在哪能下载,谢谢了!
- 关于INI文件的疑问
- 有关C#写XML的问题
- 天啊,这怎么可能?我遇到鬼了!帮帮我吧,谢谢!!!
- 关于接口、类的概念,请教大家!
- 向高手请教,如何在.NET中向SQL2000导入EXCEL数据表?
- C#web开发的问题,为何只能在本地web matrix下运行,从explorer访问时出错,有合适的答案马上给分
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();
}
}
}
dt1.Rows.Remove(dr);
dt2.Rows.Add(dr);
ds1.Merge(ds2)就可以把ds2中的所有数据及状态加进ds1中
ds1.Merge(ds2.tables[0])就可以把ds2中的的第一个表及状态加进ds1中
ds1.Merge(DataRows)就可以把DataRows的数据加进ds1,DataRows是筛选出的行的集合
DataTable newTable = oldDataSet.Tables[0].Copy();
//DataTable newTable = oldDataSet.Tables["表名"].Copy();newDateSet.Tables.Add(newTable);