使用oracle和C#

时间:2019-04-08 08:32:49

标签: c# oracle compare

我正在编写一个C#应用程序,用于比较两个不同选择的结果是否相同以及它们是执行时间,以进行优化。

实际上,我使用秒表来获取执行时间,然后将OracleDataReaders转换为DataTable并比较具有顺序独立性的行,如下所示:

        var tableA = new DataTable();
        tableA.Load(readerA);

        var tableB = new DataTable();
        tableB.Load(readerB);

        bool equals = true;
        for (int i = 0; i < tableA.Rows.Count; i++)
        {
            if (!DataRowComparer.Default.Equals(tableA.Rows[i],tableB.Rows[i]))
            {
                equals = false;
                break;
            }
        }

        return equals;

但是我假设将OracleDataReader转换为DataTable,然后使用循环比较行是相同的,并且顺序相同。

是否有使用C#和Oracle的预构建方法来比较具有/不具有行顺序的两个选择的结果?

谢谢

1 个答案:

答案 0 :(得分:1)

这里是为两个OracleDataReaders编写通用数据比较方法的尝试。该代码逐行,逐列比较阅读器以发现任何差异。考虑到读者可能包含来自多个查询的结果。如果要比较更复杂的数据类型(二进制等),则需要增强代码。该代码还假设数据的顺序很重要。如果即使在排序不同时也要认为读者是平等的,则需要重写代码以将行放入列表或字典等中。

private bool ReadersContainEqualData(OracleDataReaders readerA, OracleDataReaders readerB)
{
    bool moreResultsA = false;
    bool moreResultsB = false;
    do {
        if(readerA.FieldCount != readerB.FieldCount)
        {
            return false; // the readers have different number of columns
        }
        while(readerA.Read() && readerB.Read())
        {
            for(int i = 0; i < readerA.FieldCount; i++)
            {
                if(readerA.GetName(i) != readerB.GetName(i)) // different column names, remove this check if it is not important to you
                {
                   return false;
                }
                if(readerA[i] != readerB[i]) // the columns are either string, numeric or booean, so simple equals comparison works. If more complex columns like varbinary etc is used, this check will need to be enhanced
                {
                    return false;
                }
           }
        }
        if(readerA.Read() || readerB.Read()) // one of the readers still has more rows and the other is empty
        {
            return false;
        }

        // check if the readers contains results from another query than the recently processed
        moreResultsA = readerA.NextResult();
        moreResultsB = readerB.NextResult();
        if(moreResultsA != moreResultsB)
        {
            return false;
        }
    } while(moreResultsA && moreResultsB);
    return true;
}
相关问题