문제

창에 데이터베이스에서 레코드를 표시하는 응용 프로그램을 작성하고 몇 초마다 데이터베이스에 새 레코드를 확인합니다. 문제는 새 레코드를 확인할 때마다 창문이 깜박이고 수정하고 싶다는 것입니다. 나는 오래된 데이터 테이블을 새로운 데이터와 비교하고 다른 경우에만 새로 고침을 시도했습니다. 그러한 경우에 가장 모범 사례가 무엇인지 아는 사람이 있습니까? 나는 다음과 같은 방법으로 시도했지만 작동하지 않습니다.

private bool GetBelongingMessages()
        {
            bool result = false;
            DataTable dtTemp = OleDbWorks.GetBelongingMessages(currentCallID);
            if(dtTemp != dtMessages)
            {
                dtMessages = dtTemp;
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }
도움이 되었습니까?

해결책

우선, 코드에서 비교하는 것이 참조 DataTables의 내용물 DataTables의. 두 데이터에 대한 내용이 동일한 내용을 가지고 있는지 확인하려면 모든 행과 열을 통해 반복되어 동등한 지 확인해야합니다.

//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;
        }

다른 팁

나는 한동안 데이터 가능한 비교를 할 수있는 방법을 찾으려고 노력했고 결국 내 자신의 기능을 작성하게되었습니다. 여기에 내가 얻은 것은 다음과 같습니다.

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;

Dave Markle의 솔루션과 비교할 때 Mine은 두 개의 테이블을 동일한 레코드로하지만 다른 순서로 동일하게 처리합니다. 이것이이 스레드를 다시 우연히 발견하는 데 도움이되기를 바랍니다.

객체 t1.rows [i] [dc.columnname] 및 t1.rows [i] [dc.columnname]를 캐스트해야합니다. 그렇지 않으면 문자 t1.rows [i] [dc.columnname]! = t2.rows [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;
            }

그리고 그것은 작동하지만 우아한 솔루션은 아닙니다.

 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를 반환합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top