问题描述
如何比较另一个数据集的值。
第一个数据集 [正确记录]来自SQL Server列名称
[id],[subsNumber]
第二个数据集 [正确和不正常的记录]来自进度数据库,除了1以外的不同列,这是 subsNumber
我如何去做另外一个数据集,其中包含所有的 [subsNumber]
来自[正确的记录]与第二个数据集的匹配记录[正确的不良记录]?
或
删除第二个数据集中的所有记录[正确和不合适的记录],它们与第一个数据集
中的subsNumber列不匹配或任何其他想法
基本上如何从第二个数据集获取与第一个数据集相同的subsNumber的所有记录
关键是使用System.D ata.DataRelation在一个公共列(或列)上加入2个数据表。
这里是一些从
数据表(DataTable)和数据表(DataTable(差异));($)
尝试{
using(DataSet dataSet = new DataSet()){
dataSet.Tables.AddRange(new DataTable [] {ProperRecords.Copy(),ImproperRecords.Copy()})
DataColumn properColumn = new DataColumn();
properColumn = dataSet.Tables [0] .Columns [1]; //假设subsNumber在索引1
DataColumn unnlumnColumn = new DataColumn();
incorrectColumn = dataSet.Tables [1] .Columns [0]; //假设subsNumber在索引0
//创建DataRelation
DataRelation relation = new DataRelation(string.Empty,properColumn,incorrectColumn,false);
dataSet.Relations.Add(relation);
//为(int i = 0; i< ImproperRecords.Columns.Count; i ++){
relatedTable.Columns.Add(不正确的记录)创建返回relatedTable的列
。列[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);
}
返回relatedTable;
}
How do I compare values of one data set from another.
1st dataset ["proper records"] is coming from SQL Server with column names
[id], [subsNumber]
2nd dataset ["proper and inproper records"] is coming from progress database, with different columns except 1 which is subsNumber
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"] ?
or
delete all the records in 2nd dataset["proper and inproper records"] which don't match the "subsNumber" column in the 1st dataset
or any other idea
basically How do I get all records from 2nd dataset which has same "subsNumber" as the 1st dataset
The key is using System.Data.DataRelation to join your 2 datatables on a common column (or columns).
Here's some code derived from a post at KC's See Sharp Blog
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;
}
这篇关于比较数据集或更好的主意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!