DataTable.Merge和DataTable.ImportRow不改变的RowState [英] DataTable.Merge and DataTable.ImportRow does not change RowState

查看:439
本文介绍了DataTable.Merge和DataTable.ImportRow不改变的RowState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ADO.NET 2.0合并/导入数据的问题。我需要更新从一个普通的表/插入数据到另一个表与这两个表保持相同的架构。下面code的伟大工程在本地,但不会更改数据库:

I am having issues with ADO.NET 2.0 merging/importing data. I need to update/insert data from one generic table to another table with both tables maintaining an identical schema. The following code works great locally, but does not make changes to the database:

        OleDbDataAdapter localDA = loadLocalData();            
        OleDbDataAdapter hostedDA = loadHostedData();            

        DataSet dsLocal = new DataSet();            
        localDA.Fill(dsLocal);

        DataSet dsChanges = new DataSet();
        hostedDA.Fill(dsChanges);

        dsLocal.Tables[0].Merge(dsChanges.Tables[0],false);

        localDA.Update(dsLocal.Tables[0]);

这同样适用与此code片断:

The same is true with this code snippet:

        OleDbDataAdapter localDA = loadLocalData();
        OleDbDataAdapter hostedDA = loadHostedData();

        DataSet dsLocal = new DataSet();
        localDA.Fill(dsLocal);

        DataSet dsChanges = new DataSet();
        hostedDA.Fill(dsChanges);

        foreach (DataRow changedRow in dsChanges.Tables[0].Rows)
        {
            if (recordExists(dsLocal.Tables[0], changedRow["ID"]))
            {

            }
            else
            {
                dsLocal.Tables[0].ImportRow(changedRow);
            }
        }

        localDA.Update(dsLocal.Tables[0]);

当我看着RowState属性的改变/追加行,他们保持不变。我想,以避免数据映射如果可能的列,这是我可能会使用NEWROW()方法和修改现有行的事。

When I looked at the RowState property for changed/appended rows they remain "unchanged". I am wanting to avoid data mapping the columns if possible, which is what I may have to do using the NewRow() method and modifying an existing row.

推荐答案

您需要断火相应的功能,每一个的DataRow。

You need to fire off the appropriate function for each DataRow.

  • DataRow.SetAdded()
  • DataRow.SetModified()

这将更新DataRow.RowState值。这是一个DataAdapter的推移,以确定哪些行需要的东西对他们执行的操作。

This will update the DataRow.RowState value. This is what a DataAdapter goes by to determine which rows need what actions performed on them.

这将是很好,如果他们是像在.NET中的绑定数据集,这样它会管理这些平凡的细节我们。

It would be nice if their was something like a binded dataset in .net so that it would manage these mundane details for us.

这篇关于DataTable.Merge和DataTable.ImportRow不改变的RowState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆