public async Task Init() { PleaseClickCompare = false; Comparer = null; a7DbDataComparer structComparer = null; using (var busyVm = new BusyViewModel(AppViewModel.Instance, "Comparing database structures...")) { await Task.Factory.StartNew(() => { structComparer = new a7DbDataComparer(_connData.Name, DbA, DbB, AddLog); }); } if (structComparer != null) { Comparer = structComparer; PleaseClickCompare = false; //a7DbStructCompResultWnd wnd = new a7DbStructCompResultWnd(structComparer); //wnd.ShowDialog(); } else { MessageBox.Show("Something is wrong..."); PleaseClickCompare = true; } }
public a7ComparisonField(object valA, object valB, bool rowAExists, bool rowBExists, string name, a7DbDataComparer comparer) { ValueA = valA; ValueB = valB; RowAExists = rowAExists; RowBExists = rowBExists; Name = name; _comparer = comparer; }
public a7DbTableComparer(Table tableA, Table tableB, a7DbDataComparer dataComparer) { IsSelected = true; PrimaryKeyColumnsA = new List <string>(); PrimaryKeyColumnsB = new List <string>(); ColumnsA = new List <string>(); ColumnsB = new List <string>(); _mergeWithDelete = false; MergeDirection = a7DbComparerDirection.None; MergeAtoB = new a7LambdaCommand(async(o) => { if (MergeDirection != a7DbComparerDirection.AtoB) { await SetMergeDirection(a7DbComparerDirection.AtoB); } else { await SetMergeDirection(a7DbComparerDirection.None); } } ); MergeBtoA = new a7LambdaCommand(async(o) => { if (MergeDirection != a7DbComparerDirection.BtoA) { await SetMergeDirection(a7DbComparerDirection.BtoA); } else { await SetMergeDirection(a7DbComparerDirection.None); } } ); //AnalyzeTableCommand = new a7LambdaCommand((o) => // { AnalyzeTable().ContinueWith(t =>IsTableVisible = true); }); //HideTable = new a7LambdaCommand((o) => IsTableVisible = false); //ShowTableCommand = new a7LambdaCommand((o) => IsTableVisible = true); AnalyzeTableCommand = new a7LambdaCommand((o) => { AnalyzeTable().ContinueWith(t => this.ShowTable()); }); ShowTableCommand = new a7LambdaCommand((o) => this.ShowTable()); dataComparer.Log("Testing for differences - '" + tableA.Name + "'"); _comparer = dataComparer; _srv = dataComparer.Srv; TableA = tableA; TableB = tableB; IsOK = true; var sql = "Select * from {0}.dbo.{1}"; DataSetA = _srv.ConnectionContext.ExecuteWithResults(string.Format(sql, DbAName, tableA.Name)); DataTableA = DataSetA.Tables[0]; DataSetB = _srv.ConnectionContext.ExecuteWithResults(string.Format(sql, DbBName, tableB.Name)); DataTableB = DataSetB.Tables[0]; IsAnalyzedRows = false; IsDifferentStructure = false; var columnsInAorB = new List <string>(); //collect column and primary keys info var pkColumnsA = new List <DataColumn>(); foreach (var clA in tableA.Columns) { var col = clA as Column; if (col.InPrimaryKey) { pkColumnsA.Add(DataTableA.Columns[col.Name]); PrimaryKeyColumnsA.Add(col.Name); } ColumnsA.Add(col.Name); if (!columnsInAorB.Contains(col.Name)) { columnsInAorB.Add(col.Name); } } DataTableA.PrimaryKey = pkColumnsA.ToArray(); var pkColumnsB = new List <DataColumn>(); foreach (var clB in tableB.Columns) { var col = clB as Column; if (col.InPrimaryKey) { pkColumnsB.Add(DataTableB.Columns[col.Name]); PrimaryKeyColumnsB.Add(col.Name); } ColumnsB.Add(col.Name); if (!columnsInAorB.Contains(col.Name)) { columnsInAorB.Add(col.Name); } } DataTableB.PrimaryKey = pkColumnsB.ToArray(); //compare primary keys if the same in both databases if (pkColumnsA.Count == 0 && pkColumnsB.Count == 0) { IsOK = false; IsDifferentData = true; ErrorText = string.Format("{2}: Table in {0} and in {1} has no primary keys.", DbAName, DbBName, TableName); } else if (pkColumnsA.Count == 0) { IsOK = false; IsDifferentData = true; ErrorText = string.Format("{1}: Table in {0} has no primary keys.", DbAName, TableName); } else if (pkColumnsB.Count == 0) { IsOK = false; IsDifferentData = true; ErrorText = string.Format("{1}: Table in {0} has no primary keys.", DbBName, TableName); } else if (pkColumnsA.Count != pkColumnsB.Count) { IsOK = false; IsDifferentData = true; ErrorText = TableName + ": Different amount of primary key columns in both databases."; } else { var differentPk = false; for (var i = 0; i < pkColumnsA.Count; i++) { if (pkColumnsA[i].ColumnName != pkColumnsB[i].ColumnName) { differentPk = true; break; } } if (differentPk) { IsOK = false; IsDifferentData = true; ErrorText = TableName + ": Primary key columns in both databases are different."; } } if (IsOK) { //summary of column information ColumnsOnlyInA = new ObservableCollection <string>(); ColumnsOnlyInB = new ObservableCollection <string>(); ColumnsInBothTables = new ObservableCollection <string>(); foreach (var cn in columnsInAorB) { if (!ColumnsA.Contains(cn) && ColumnsB.Contains(cn)) { ColumnsOnlyInB.Add(cn); continue; } if (!ColumnsB.Contains(cn) && ColumnsA.Contains(cn)) { ColumnsOnlyInA.Add(cn); continue; } if (ColumnsA.Contains(cn) && ColumnsB.Contains(cn)) { ColumnsInBothTables.Add(cn); } } var pkA = DataTableA.PrimaryKey; var pkB = DataTableB.PrimaryKey; IsDifferentData = false; //compare the rows of the two tables if (ColumnsOnlyInA.Count > 0 || ColumnsOnlyInB.Count > 0) {//different number of columns IsDifferentData = true; IsDifferentStructure = true; } else if (DataTableA.Rows.Count != DataTableB.Rows.Count) {//different number of rows IsDifferentData = true; } else if (pkA.Length != pkB.Length) {//different number of primary key columns IsDifferentData = true; } else { //compare the row data foreach (var rwA in DataTableA.Rows) { //foreach row in dtA var rowA = rwA as DataRow; var pkValues = new List <object>(); foreach (var pk in pkA) { pkValues.Add(rowA[pk.ColumnName]); } //get the row in dtB var rowB = DataTableB.Rows.Find(pkValues.ToArray()); if (rowB == null) {//no row in dtB for this row IsDifferentData = true; break; } else { //compare the field values var isDataDifferentInRow = false; foreach (var cn in ColumnsInBothTables) { if (cn != "dbVersion" && cn != "dbTimestamp" && cn != "dbUser") { if (rowA[cn]?.ToString() != rowB[cn]?.ToString()) { isDataDifferentInRow = true; break; } } } if (isDataDifferentInRow) { IsDifferentData = true; break; } } } } } }
public a7ComparisonRow(DataRow rowA, DataRow rowB, a7DbTableComparer tableComparer, a7DbDataComparer comparer) { MergeDirection = a7DbComparerDirection.None; MergeAtoB = new a7LambdaCommand((o) => { if (MergeDirection != a7DbComparerDirection.AtoB) { SetMergeDirection(a7DbComparerDirection.AtoB, false); } else { SetMergeDirection(a7DbComparerDirection.None, false); } } ); MergeBtoA = new a7LambdaCommand((o) => { if (MergeDirection != a7DbComparerDirection.BtoA) { SetMergeDirection(a7DbComparerDirection.BtoA, false); } else { SetMergeDirection(a7DbComparerDirection.None, false); } } ); _dataRowA = rowA; _dataRowB = rowB; _comparer = comparer; _tblComparer = tableComparer; if (_dataRowA == null && _dataRowB != null) { IsOnlyInB = true; IsOnlyInA = false; AtoBCaption = "B>X"; BtoACaption = "B>A"; } else if (_dataRowB == null && _dataRowA != null) { IsOnlyInB = false; IsOnlyInA = true; AtoBCaption = "A>B"; BtoACaption = "A>X"; } else { IsOnlyInA = false; IsOnlyInB = false; AtoBCaption = "A>B"; BtoACaption = "B>A"; } Fields = new Dictionary <string, a7ComparisonField>(); IsDifferent = false; for (var i = 0; i < tableComparer.ColumnsInBothTables.Count; i++) { var colName = tableComparer.ColumnsInBothTables[i]; object valA = null; if (rowA != null) { valA = rowA[colName]; } object valB = null; if (rowB != null) { valB = rowB[colName]; } Fields.Add(colName, new a7ComparisonField(valA, valB, rowA != null, rowB != null, colName, this._comparer)); if (valA?.ToString() != valB?.ToString() && colName != "dbVersion" && colName != "dbTimestamp" && colName != "dbUser") { IsDifferent = true; } } for (var i = 0; i < tableComparer.ColumnsOnlyInA.Count; i++) { var colName = tableComparer.ColumnsOnlyInA[i]; object valA = null; if (rowA != null) { valA = rowA[colName]; } Fields.Add(colName, new a7ComparisonField(valA, null, true, false, colName, this._comparer)); IsDifferent = true; } for (var i = 0; i < tableComparer.ColumnsOnlyInB.Count; i++) { var colName = tableComparer.ColumnsOnlyInB[i]; object valB = null; if (rowB != null) { valB = rowB[colName]; } Fields.Add(colName, new a7ComparisonField(null, valB, false, true, colName, this._comparer)); IsDifferent = true; } }