DataTable.GetChanges()保持返回NULLDataTable、GetChanges、NULL

2023-09-03 04:22:04 作者:毀滅 world

我试图让所有存在于 ALLDATA 的行,但不是在 removeData

 公共静态数据表RemoveDuplicateRows(数据表ALLDATA,
    数据表removeData)
{
    removeData.Merge(ALLDATA);
    数据表newData = removeData.GetChanges();
    removeData.RejectChanges();
    返回newData;
}
 

removeData 优先于空在这种情况下,调用(只是一个新的DataTable();)

newData 总是空的数据表newData = removeData.GetChanges后的值();

最终解决方案:

 公共静态数据表RemoveDuplicateRows(数据表ALLDATA,数据表removeData)
{
数据表重复= allData.Clone();
的foreach(DataRow的行allData.Rows)
{
duplicate.ImportRow(行);
}
的foreach(DataRow的行duplicate.Rows)
{
row.SetAdded();
}

removeData.Merge(一式两份);
数据表newData = removeData.GetChanges(DataRowState.Added);
removeData.RejectChanges();
allData.RejectChanges();
返回newData;
}
 

解决方案 8. 持续交付,持续部署,傻傻分不清楚

removeData 数据表需要有相同的列/为 ALLDATA 。换句话说,它不能只是一个新的DataTable()。

I am trying to get all the rows that exist in allData but not in removeData

public static DataTable RemoveDuplicateRows(DataTable allData, 
    DataTable removeData) 
{
    removeData.Merge(allData);
    DataTable newData = removeData.GetChanges(); 
    removeData.RejectChanges();
    return newData;
}

removeData is empty prior to the call in this case (just a new DataTable();)

But newData always has a value of null after the DataTable newData = removeData.GetChanges(); line

Final Solution:

	public static DataTable RemoveDuplicateRows(DataTable allData, DataTable removeData) 
	{
		DataTable duplicate = allData.Clone();
		foreach (DataRow row in allData.Rows)
		{
			duplicate.ImportRow(row);
		}
		foreach (DataRow row in duplicate.Rows)
		{
			row.SetAdded();
		} 

		removeData.Merge(duplicate);
		DataTable newData = removeData.GetChanges(DataRowState.Added);
		removeData.RejectChanges();
		allData.RejectChanges();
		return newData;
	}

解决方案

Your removeData DataTable needs to have the same columns/fields as allData. In other words, it can't just be a new DataTable().