public static bool ClearAndMirrorCompleteDataSetTo(WCFStandardsNS.WCFStandards TargetDataBase, DataSet DataSetToMirror) { if (DataSetToMirror == null) return true; foreach (DataTable SourceTable in DataSetToMirror.Tables) { String DeleteCommand = "Delete from " + SourceTable.TableName; TargetDataBase.RunSQLBatch(DeleteCommand); } return MirrorDataSetTo(TargetDataBase, DataSetToMirror); }
public static bool MirrorDataSetTo(WCFStandardsNS.WCFStandards TargetDataBase, DataSet DataSetToMirror) { if (DataSetToMirror == null) return true; foreach (DataTable SourceTable in DataSetToMirror.Tables) { DataTable DeletedRows = SourceTable.GetChanges(DataRowState.Deleted); if (DeletedRows != null) foreach (DataRow ToDelete in DeletedRows.Rows) { if (TargetDataBase.GetCommonDataSet ("Select ID from " + SourceTable.TableName + " where ID = '" + ToDelete["ID"].ToString() + "'") .Tables[SourceTable.TableName].Rows.Count > 0) { String DeleteCommand = "Delete from " + SourceTable.TableName + " where ID = '" + ToDelete["ID"].ToString() + "'"; TargetDataBase.RunSQLBatch(DeleteCommand); } } List<String> SourceIDs = new List<string>(); foreach (DataRow SourceRow in SourceTable.Rows) if (SourceRow.RowState != DataRowState.Deleted) SourceIDs.Add(SourceRow["ID"].ToString()); if (SourceIDs.Count == 0) continue; String WhereClause = " where (ID = '" + String.Join("' or ID = '", SourceIDs.ToArray()) + "') "; DataSet TargetDataSet = TargetDataBase.GetCommonDataSet("Select * from " + SourceTable.TableName + WhereClause); if (TargetDataSet == null) { WMB.Basics.ReportErrorToEventViewer("MBRStatics.MirrorDataSetTo", "Bei der Table \"" + SourceTable.TableName + "\" konnte nichts gelesen werden"); return false; } foreach (DataRow SourceRow in SourceTable.Rows) { DataRow[] TargetRows = TargetDataSet.Tables[SourceTable.TableName] .Select("ID = '" + SourceRow["ID"].ToString() + "'"); if (TargetRows.Length > 1) { WMB.Basics.ReportErrorToEventViewer("MBRStatics.MirrorDataSetTo", "Bei der Table \"" + SourceTable.TableName + "\" für die ID \"" + SourceRow["ID"].ToString() + "\" gibt es mehr als einen Record!!!"); return false; } if (TargetRows.Length > 1) { WMB.Basics.ReportErrorToEventViewer("MBRStatics.MirrorDataSetTo", "Bei der Tabelle \"" + SourceTable.TableName + "\"\r\n" + "Bei der ID \"" + SourceRow["ID"].ToString() + "\" tritt ein Datenbank Fehler auf.\r\n" + "Mehr als ein Record für diese ID"); continue; } DataRow TargetRow = null; if (TargetRows.Length < 1) { TargetRow = TargetDataSet.Tables[SourceTable.TableName].NewRow(); TargetRow["ID"] = SourceRow["ID"].ToString(); TargetRow["LastUpdateToken"] = WMB.Basics.GetNextLastUpdateTokenHelper(); TargetDataSet.Tables[SourceTable.TableName].Rows.Add(TargetRow); } else { TargetRow = TargetRows[0]; } foreach (DataColumn Col in TargetDataSet.Tables[SourceTable.TableName].Columns) { if (Col.ColumnName != "ID") if (String.Compare(TargetRow[Col.ColumnName].ToString(), SourceRow[Col.ColumnName].ToString()) != 0) { if (Convert.IsDBNull(SourceRow[Col.ColumnName]) == true) TargetRow[Col.ColumnName] = Convert.DBNull; else TargetRow[Col.ColumnName] = SourceRow[Col.ColumnName].ToString(); } } } String ReturnMessage = TargetDataBase.SetCommonDataSet(TargetDataSet); if (!String.IsNullOrEmpty(ReturnMessage)) { WMB.Basics.ReportErrorToEventViewer("MBRStatics.MirrorDataSetTo", "Bei der Table \"" + SourceTable.TableName + "\" kam es zu folgendem Fehler:\r\n" + ReturnMessage); return false; } } return true; }
public static String ModifyThisRow(WCFStandardsNS.WCFStandards DataBase, DataRow RowToStore) { if (RowToStore == null) return String.Empty; String TableName = RowToStore.Table.TableName; List<String> SetCommandPart = new List<string>(); foreach (DataColumn Col in RowToStore.Table.Columns) { if (Col.ColumnName == "ID") continue; if (RowToStore[Col.ColumnName] == Convert.DBNull) { SetCommandPart.Add(Col.ColumnName + " = NULL"); continue; } if ((Col.DataType == typeof(Int32)) || (Col.DataType == typeof(Int64)) || (Col.DataType == typeof(Double)) ) { SetCommandPart.Add(Col.ColumnName + " = " + RowToStore[Col.ColumnName].ToString()); continue; } if (Col.DataType == typeof(DateTime)) { SetCommandPart.Add(Col.ColumnName + " = Convert (DateTime, '" + Convert.ToDateTime(RowToStore[Col.ColumnName]) .ToString(WMB.Basics.ISO_DATE_TIME_FORMAT) + "', 120)"); continue; } SetCommandPart.Add(Col.ColumnName + " = '" + RowToStore[Col.ColumnName].ToString() + "'"); } String UpdateStatement = "Update " + RowToStore.Table.TableName + " set " + String.Join(", ", SetCommandPart.ToArray()) + " where ID = '" + RowToStore["ID"].ToString() + "'"; return DataBase.RunSQLBatch(UpdateStatement); }