如下:
DataSet dt = new DataSet();
dt =dtSource;//直接赋值即可
//更新使用DataAdapter
SqlDataAdapter.Update(dt);
DataSet dt = new DataSet();
dt =dtSource;//直接赋值即可
//更新使用DataAdapter
SqlDataAdapter.Update(dt);
解决方案 »
- C#动态生成winForm窗体,并使用窗体控件的属性和控件事件
- 怎么把一个string 中的数字和小数点提取出来?用正则表达式可以吗?
- C#如何把一byte[] 加载入XmlDocument,或是创建一个Stream
- 关于类型转换的一个疑惑
- 命令行程序
- 为什么页面上的按钮点击确没有任何反应(100分不够再加)
- 关于禁止重复调用一个DLL中某个窗体的方法?
- 使用smtp發送外部郵件的問題
- 请问com组件操作WORD 插入图片一定是必须本地的吗 Selection.InlineShapes.AddPicture
- win10下ComboBox颜色
- 为何设了PrimaryKey运行出错说没有设?哪里不对?
- 关于MessageBox
另一个DataSet ds2
将数据库表中数据取出形成一个Dataset(ds2),使用ds2.Merge(ds1),融合数据,然后象数据库提交。例子
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();
}
}
}
另:联机帮助里关于Merge方法说明很详细