Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较数据表_C#_Datatable - Fatal编程技术网

C# 比较数据表

C# 比较数据表,c#,datatable,C#,Datatable,我构建了一个应用程序,它在窗口中显示数据库中的记录,并每隔几秒钟检查数据库中的新记录。问题是,每次我检查新记录并想要修复它时,窗口都会闪烁。我尝试将旧的数据表与新的数据表进行比较,只有在它们不同时才刷新。 有人知道这种情况的最佳做法是什么吗?我试着用下面的方法来做,但不起作用: private bool GetBelongingMessages() { bool result = false; DataTable dtTemp =

我构建了一个应用程序,它在窗口中显示数据库中的记录,并每隔几秒钟检查数据库中的新记录。问题是,每次我检查新记录并想要修复它时,窗口都会闪烁。我尝试将旧的数据表与新的数据表进行比较,只有在它们不同时才刷新。 有人知道这种情况的最佳做法是什么吗?我试着用下面的方法来做,但不起作用:

private bool GetBelongingMessages()
        {
            bool result = false;
            DataTable dtTemp = OleDbWorks.GetBelongingMessages(currentCallID);
            if(dtTemp != dtMessages)
            {
                dtMessages = dtTemp;
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }

首先,必须认识到,在代码中比较的是数据表的引用,而不是数据表的内容。为了确定两个数据表是否具有相同的内容,您必须遍历所有的行和列,看看它们是否相等:

//This assumes the datatables have the same schema...
        public bool DatatablesAreSame(DataTable t1, DataTable t2) {         
            if (t1.Rows.Count != t2.Rows.Count)
                return false;

            foreach (DataColumn dc in t1.Columns) {
                for (int i = 0; i < t1.Rows.Count; i++) {
                    if (t1.Rows[i][dc.ColumnName] != t2.Rows[i][dc.ColumnName]) {
                        return false;
                    }
                }
            }
            return true;
        }
//这假设数据表具有相同的架构。。。
public bool datatablesareame(DataTable t1,DataTable t2){
if(t1.Rows.Count!=t2.Rows.Count)
返回false;
foreach(t1.Columns中的数据列dc){
对于(int i=0;i
必须强制转换对象t1.Rows[i][dc.ColumnName]和t1.Rows[i][dc.ColumnName],否则语句t1.Rows[i][dc.ColumnName]!=t2.行[i][dc.ColumnName]始终为真。我以以下方式修改了代码:

for(int i = 0; i < t1.Rows.Count; i++)
            {
                if((string)t1.Rows[i][1] != (string)t2.Rows[i][1])
                    return false;
            }
for(int i=0;i

它可以工作,但不是一个优雅的解决方案。

我一直在寻找一种方法来进行数据表比较,最后编写了自己的函数,下面是我得到的:

bool tablesAreIdentical = true;

// loop through first table
foreach (DataRow row in firstTable.Rows)
{
    foundIdenticalRow = false;

    // loop through tempTable to find an identical row
    foreach (DataRow tempRow in tempTable.Rows)
    {
        allFieldsAreIdentical = true;

        // compare fields, if any fields are different move on to next row in tempTable
        for (int i = 0; i < row.ItemArray.Length && allFieldsAreIdentical; i++)
        {
            if (!row[i].Equals(tempRow[i]))
            {
                allFieldsAreIdentical = false;
            }
        }

        // if an identical row is found, remove this row from tempTable 
        //  (in case of duplicated row exist in firstTable, so tempTable needs
        //   to have the same number of duplicated rows to be considered equivalent)
        // and move on to next row in firstTable
        if (allFieldsAreIdentical)
        {
            tempTable.Rows.Remove(tempRow);
            foundIdenticalRow = true;
            break;
        }
    }
    // if no identical row is found for current row in firstTable, 
    // the two tables are different
    if (!foundIdenticalRow)
    {
        tablesAreIdentical = false;
        break;
    }
}

return tablesAreIdentical;
bool-tablesarelevant=true;
//循环遍历第一个表
foreach(firstTable.Rows中的数据行)
{
foundIdenticalRow=false;
//循环查找相同的行
foreach(testable.Rows中的DataRow tempRow)
{
AllFieldsAridentical=true;
//比较字段,如果有不同的字段,请转到“诱惑”中的下一行
对于(int i=0;i
与Dave Markle的解决方案相比,我的解决方案将具有相同记录但顺序不同的两个表视为相同的。希望这能帮助那些再次偶然发现这条线索的人

 public Boolean CompareDataTables(DataTable table1, DataTable table2)
   {
       bool flag = true;
       DataRow[] row3 = table2.Select();
       int i = 0;// row3.Length;
       if (table1.Rows.Count == table2.Rows.Count)
       {
           foreach (DataRow row1 in table1.Rows)
           {
               if (!row1.ItemArray.SequenceEqual(row3[i].ItemArray))
               {
                   flag = false;
                   break;
               }
               i++;
           }

       }
       else
       {
           flag = false;
       }
       return flag;
   }

//在这里,此函数将给出布尔值作为结果,如果两者相同,则返回true;如果两者不相同,则返回false

此函数可能不起作用,但它是解决方案。我会把上面的答案标记为这个问题的答案,因为没有比这更好的了我建议像这样添加“ToString”方法:
if(t1.Rows[I][dc.ColumnName].ToString()!=t2.Rows[I][dc.ColumnName].ToString())
。比较可能会得出错误答案,答案为35!=35