比较数据集或更好的想法

Compare dataset or a better idea(比较数据集或更好的想法)

本文介绍了比较数据集或更好的想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较一个数据集与另一个数据集的值.

How do I compare values of one data set from another.

第一个数据集 ["proper records"] 来自 SQL Server 的列名

1st dataset ["proper records"] is coming from SQL Server with column names

 [id], [subsNumber]

2nd dataset ["proper and inproper records"] 来自进度数据库,除了 1 之外的其他列是 subsNumber

2nd dataset ["proper and inproper records"] is coming from progress database, with different columns except 1 which is subsNumber

如何制作另一个数据集,其中包含来自 ["proper records"] 的所有 [subsNumber] 与来自 2nd datset ["proper inproper records"] 的匹配记录?

How do I go and make another dataset which has all the [subsNumber] from ["proper records"] with matching records from 2nd datset ["proper inproper records"] ?

删除第二个数据集中的所有记录[正确和不正确的记录"]与第一个数据集中的subsNumber"列不匹配

delete all the records in 2nd dataset["proper and inproper records"] which don't match the "subsNumber" column in the 1st dataset

或任何其他想法

基本上我如何从与第一个数据集具有相同subsNumber"的第二个数据集中获取所有记录

basically How do I get all records from 2nd dataset which has same "subsNumber" as the 1st dataset

推荐答案

关键是使用 System.Data.DataRelation 将您的 2 个数据表连接到一个(或多个)公共列上.

The key is using System.Data.DataRelation to join your 2 datatables on a common column (or columns).

这里有一些代码来自 KC 的查看 Sharp 博客

public DataTable GetImproperRecords(DataTable ProperRecords, DataTable ImproperRecords) {
  DataTable relatedTable = new DataTable("Difference");
  try {
     using (DataSet dataSet = new DataSet()) {
        dataSet.Tables.AddRange(new DataTable[] { ProperRecords.Copy(), ImproperRecords.Copy() });

        DataColumn properColumn = new DataColumn();
        properColumn = dataSet.Tables[0].Columns[1]; // Assuming subsNumber is at index 1

        DataColumn improperColumn = new DataColumn();
        improperColumn = dataSet.Tables[1].Columns[0]; // Assuming subsNumber is at index 0

        //Create DataRelation
        DataRelation relation = new DataRelation(string.Empty, properColumn, improperColumn, false);

        dataSet.Relations.Add(relation);

        //Create columns for return relatedTable
        for (int i = 0; i < ImproperRecords.Columns.Count; i++) {
           relatedTable.Columns.Add(ImproperRecords.Columns[i].ColumnName, ImproperRecords.Columns[i].DataType);
        }

        relatedTable.BeginLoadData();

        foreach (DataRow parentrow in dataSet.Tables[1].Rows) {
           DataRow[] childrows = parentrow.GetChildRows(relation);

           if (childrows != null && childrows.Length > 0)
              relatedTable.LoadDataRow(parentrow.ItemArray, true);

        }

        relatedTable.EndLoadData();

     }
  }
  catch (Exception ex) {
     Console.WriteLine(ex.Message);
  }

  return relatedTable;
}

这篇关于比较数据集或更好的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:比较数据集或更好的想法

基础教程推荐