【问题标题】:Merging Multiple ADO.NET DataSets合并多个 ADO.NET 数据集
【发布时间】:2023-04-08 01:43:01
【问题描述】:

场景

我有一个带有 gridview 的 c# winforms 应用程序。 gridview 数据源由数据集填充。 该数据集的内容每隔几秒钟就会更新一次,并且数据集中的项目数量并不总是相同等。 数据集实际上是通过一次传入多个其他数据集来填充(或需要填充)的。这些数据集在完全随机的时间更新,这是无法控制的。

问题

由于本质上 GridView 数据集正在“持续更新” - 在更新时(其中包含最新信息的新数据集需要与现有 GridView 数据源合并),我需要采取填充 gridview 的当前数据集的副本,将其与来自其他数据集的传入数据进行比较,并仅更新新数据。

到目前为止

我尝试了一系列不同的方法,但我似乎无法让它发挥作用。我要么最终将数据集不断地添加到具有略微不同数据的副本中,要么随机删除行(我尝试使用仅偶尔有效的合并全部然后删除的方法)。

有什么办法可以解决这个问题!?

目前,gridview 只是更新自身以显示最新传入的数据集,而没有将先前的数据集合并到其中.....

代码 - 这会随机删除行,即使它在某些时候会显示所有数据

//If the existing GridView dataset contains any rows that 
//exist in the incoming latest dataset (matched on the two unique fields
//of the dataset), then remove that row from the existing GridView dataset
for (int i = 0; i < existingPriceResultsDTCopyForCompare.Rows.Count; i++)
{
    foreach (DataRow incomingRow in incomingDS.Tables[0].Rows)
    {
        string incomingDate = incomingRow["Date"].ToString();

        DataRow currentRow = existingPriceResultsDTCopyForCompare.Rows[i];
        if ((currentRow["CCY"].ToString().Contains(incomingCcy))
            && (currentRow["Date"].ToString().Contains(incomingDate)))
        {
            existingPriceResultsDT.Rows.RemoveAt(i);

        }
    }
}

//Then merge the existing GridView dataset (Minus the old data that 
//matches the data from the incoming Dataset with the latest information),
//With the brand new incoming data.
incomingDS.Merge(existingPriceResultsDT);

编辑-

我开始怀疑传入的数据集是否会在迭代有时间完成之前一直被下一个传入的数据集覆盖。所以我猜我需要锁定传入的数据集?

【问题讨论】:

  • 我们是否可以假设上述代码示例中指向的所有数据集在您迭代它们时都是稳定的?

标签: c# .net dataset


【解决方案1】:

你有没有尝试过类似的东西

  DataSet ds1 = new DataSet();
  DataSet ds2 = new DataSet();   

  ds1.Merge(ds2);    
  DataSet ds3 = ds1.GetChanges();

根据您的对象

  DataSet existingPriceResultsDT = new DataSet();
  DataSet incomingDS = new DataSet();

  incomingDS.Merge(existingPriceResultsDT);
  existingPriceResultsDT = incomingDS.GetChanges();

【讨论】:

    【解决方案2】:

    您可能想查看Microsoft Sync Framework。这听起来像是一个完美的场景。这个video 是一个很好的介绍。也可以下载教程here

    【讨论】:

      【解决方案3】:

      嵌套的 for 循环是一场噩梦。您肯定不想再循环遍历这些行 - 大量不必要的比较。

      看起来您每次都在比较几列 - “CCY”和“日期”。您是否考虑过将它们用作表的主键?如果这对您的场景有意义,那么您可以使事情变得更加高效。

      你可以考虑做这样的事情:

      确定哪些列是主键 - 此处显示 CCY 和 Date 适合您。

      DataColumn[] keys = new DataColumn[2];
      keys[0] = dataTable.column["CCY"];
      keys[1] = dataTable.column["Date"];
      dataTable.PrimaryKey = keys;
      

      然后当你读入一个新的数据集时——不管你怎么做,都像这样调用 Merge:

      dataSet.Merge(newDataSet, false, MissingSchemaAction.Add);
      

      这里假设dataTable是dataSet的Tables[0]。

      至少对于我的快速测试程序,这将合并两个数据集并更新更改的行并添加任何新行。

      而且,我的 DataGridView 设置如下:

      dataGridView1.DataSource = dataSet.Tables[0];
      

      似乎为我的测试程序更新了足够好的视图。

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2017-03-26
        • 2011-02-21
        • 1970-01-01
        • 1970-01-01
        • 2016-09-27
        • 1970-01-01
        • 1970-01-01
        • 2017-01-31
        • 1970-01-01
        相关资源
        最近更新 更多