private static DataSet BuildRelationDataSet(string tablaCPD, string tablaSIGUA, DataTable joinData, DataTable fieldMappingData, TipoComprobacion direccion) { DataSet oDs = new DataSet(); DataRelation join = null; List <DataColumn> parentCols = new List <DataColumn>(); List <DataColumn> childCols = new List <DataColumn>(); DataTable TablaPadre = null; DataTable Pendientes = null; string sqlCPD = tablaCPD.Trim().ToUpper(); string sqlSIGUA = tablaSIGUA.Trim().ToUpper(); try { if (!sqlCPD.StartsWith("SELECT")) { sqlCPD = string.Format("SELECT * FROM {0}", sqlCPD); } if (!sqlSIGUA.StartsWith("SELECT")) { sqlSIGUA = string.Format("SELECT * FROM {0}", sqlSIGUA); } oDs.Tables.Add(GetDataSet(dbOrigen.ORA, sqlCPD, null).Tables[0].Copy()); if (oDs.Tables[0].TableName == "Excepcion") { string msg = (string)oDs.Tables[0].Rows[0]["Mensaje"]; throw new ApplicationException(msg); } oDs.Tables[0].TableName = "TablaCPD"; oDs.Tables.Add(GetDataSet(dbOrigen.PGSQL, sqlSIGUA, null).Tables[0].Copy()); if (oDs.Tables[1].TableName == "Excepcion") { string msg = (string)oDs.Tables[1].Rows[0]["Mensaje"]; throw new ApplicationException(msg); } oDs.Tables[1].TableName = "TablaSIGUA"; foreach (DataRow joinRow in joinData.Rows) { string CampoCPD = (string)joinRow["CampoCPD"]; string CampoSIGUA = (string)joinRow["CampoSIGUA"]; if (oDs.Tables[0].Columns[CampoCPD].DataType == typeof(System.Decimal)) { ConvertDecimalColumn(oDs.Tables[0], CampoCPD, oDs.Tables[1].Columns[CampoSIGUA].DataType); } switch (direccion) { case (TipoComprobacion.AltasPendientesEnSIGUA): parentCols.Add(oDs.Tables[0].Columns[CampoCPD]); childCols.Add(oDs.Tables[1].Columns[CampoSIGUA]); break; case (TipoComprobacion.BajasPendientesEnSIGUA): parentCols.Add(oDs.Tables[1].Columns[CampoSIGUA]); childCols.Add(oDs.Tables[0].Columns[CampoCPD]); break; case (TipoComprobacion.ModificacionesPendientesEnSIGUA): parentCols.Add(oDs.Tables[1].Columns[CampoSIGUA]); childCols.Add(oDs.Tables[0].Columns[CampoCPD]); break; } } join = oDs.Relations.Add("Join", parentCols.ToArray(), childCols.ToArray(), false); switch (direccion) { case (TipoComprobacion.AltasPendientesEnSIGUA): TablaPadre = oDs.Tables["TablaCPD"]; Pendientes = oDs.Tables["TablaCPD"].Clone(); Pendientes.TableName = "Pendientes"; break; case (TipoComprobacion.BajasPendientesEnSIGUA): TablaPadre = oDs.Tables["TablaSIGUA"]; Pendientes = oDs.Tables["TablaSIGUA"].Clone(); Pendientes.TableName = "Pendientes"; break; case (TipoComprobacion.ModificacionesPendientesEnSIGUA): TablaPadre = oDs.Tables["TablaSIGUA"]; break; } foreach (DataRow parentRow in TablaPadre.Rows) { switch (direccion) { case (TipoComprobacion.AltasPendientesEnSIGUA): if (parentRow.GetChildRows(join).Length == 0) { Pendientes.Rows.Add(parentRow.ItemArray); } break; case (TipoComprobacion.BajasPendientesEnSIGUA): if (parentRow.GetChildRows(join).Length == 0) { Pendientes.Rows.Add(parentRow.ItemArray); } break; case (TipoComprobacion.ModificacionesPendientesEnSIGUA): string CampoSIGUA = string.Empty; string CampoCPD = string.Empty; object newVal; if (parentRow.GetChildRows(join).Length > 0) { foreach (DataRow fieldMappingRow in fieldMappingData.Rows) { CampoSIGUA = (string)fieldMappingRow["CampoSIGUA"]; CampoCPD = (string)fieldMappingRow["CampoCPD"]; newVal = null; if (parentRow.GetChildRows(join)[0][CampoCPD] == System.DBNull.Value) { if (parentRow[CampoSIGUA] != System.DBNull.Value) { newVal = System.DBNull.Value; } } else { if (TablaPadre.Columns[CampoSIGUA].DataType == typeof(System.Boolean)) { if (parentRow[CampoSIGUA] == System.DBNull.Value) { newVal = (bool)parentRow.GetChildRows(join)[0][CampoCPD]; } else if ((bool)parentRow[CampoSIGUA] != (bool)parentRow.GetChildRows(join)[0][CampoCPD]) { newVal = (bool)parentRow.GetChildRows(join)[0][CampoCPD]; } } if (TablaPadre.Columns[CampoSIGUA].DataType == typeof(System.Int16)) { if (parentRow[CampoSIGUA] == System.DBNull.Value) { newVal = (System.Int16)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Int16)); } else if ((System.Int16)parentRow[CampoSIGUA] != (System.Int16)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Int16))) { newVal = (System.Int16)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Int16)); } } if (TablaPadre.Columns[CampoSIGUA].DataType == typeof(System.Int32)) { if (parentRow[CampoSIGUA] == System.DBNull.Value) { newVal = (System.Int32)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Int32)); } else if ((System.Int32)parentRow[CampoSIGUA] != (System.Int32)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Int32))) { newVal = (System.Int32)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Int32)); } } if (TablaPadre.Columns[CampoSIGUA].DataType == typeof(System.Int64)) { if (parentRow[CampoSIGUA] == System.DBNull.Value) { newVal = (System.Int64)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Int64)); } else if ((System.Int64)parentRow[CampoSIGUA] != (System.Int64)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Int64))) { newVal = (System.Int64)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Int64)); } } if (TablaPadre.Columns[CampoSIGUA].DataType == typeof(System.Double)) { if (parentRow[CampoSIGUA] == System.DBNull.Value) { newVal = (System.Double)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Double)); } else if ((System.Double)parentRow[CampoSIGUA] != (System.Double)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Double))) { newVal = (System.Double)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Double)); } } if (TablaPadre.Columns[CampoSIGUA].DataType == typeof(System.Single)) { if (parentRow[CampoSIGUA] == System.DBNull.Value) { newVal = (System.Single)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Single)); } else if ((System.Single)parentRow[CampoSIGUA] != (System.Single)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Single))) { newVal = (System.Single)SafeCast(parentRow.GetChildRows(join)[0][CampoCPD], typeof(System.Single)); } } if (TablaPadre.Columns[CampoSIGUA].DataType == typeof(System.String)) { if (parentRow[CampoSIGUA] == System.DBNull.Value) { newVal = (string)parentRow.GetChildRows(join)[0][CampoCPD]; } else if ((string)parentRow[CampoSIGUA] != (string)parentRow.GetChildRows(join)[0][CampoCPD]) { newVal = (string)parentRow.GetChildRows(join)[0][CampoCPD]; } } } if (newVal != null) { parentRow.BeginEdit(); parentRow[CampoSIGUA] = newVal; parentRow.EndEdit(); } } } break; } } switch (direccion) { case (TipoComprobacion.AltasPendientesEnSIGUA): Pendientes.AcceptChanges(); oDs.Relations.Clear(); oDs.Tables.Clear(); oDs.Tables.Add(Pendientes); break; case (TipoComprobacion.BajasPendientesEnSIGUA): Pendientes.AcceptChanges(); oDs.Relations.Clear(); oDs.Tables.Clear(); oDs.Tables.Add(Pendientes); break; case (TipoComprobacion.ModificacionesPendientesEnSIGUA): oDs.Relations.Clear(); oDs.Tables.Remove("TablaCPD"); break; } } catch (Exception e) { oDs.Relations.Clear(); oDs.Tables.Clear(); FillExceptionDataSet(oDs, e.ToString()); } return(oDs); }
public static List <object[]> GetUnjoined(DataTable TablaPadre, string CampoPadre, DataTable TablaHijo, string CampoHijo, TipoComprobacion direccion) { List <object[]> r = new List <object[]>(); DataSet oDs = new DataSet(); DataRelation join = null; try { oDs.Tables.Add(TablaPadre); oDs.Tables.Add(TablaHijo); join = oDs.Relations.Add("Join", TablaPadre.Columns[CampoPadre], TablaHijo.Columns[CampoHijo], false); foreach (DataRow parentRow in TablaPadre.Rows) { switch (direccion) { case TipoComprobacion.BajasPendientesEnSIGUA: if (parentRow.GetChildRows(join).Length > 0) { r.Add(parentRow.ItemArray); } else { parentRow.Delete(); } break; case TipoComprobacion.AltasPendientesEnSIGUA: if (parentRow.GetChildRows(join).Length == 0) { r.Add(parentRow.ItemArray); } else { parentRow.Delete(); } break; case TipoComprobacion.ModificacionesPendientesEnSIGUA: if (parentRow.GetChildRows(join).Length == 0) { if (parentRow.RowState == DataRowState.Modified) { r.Add(parentRow.ItemArray); } } else { parentRow.Delete(); } break; } } TablaPadre.AcceptChanges(); oDs.Relations.Clear(); oDs.Tables.Remove(TablaHijo); } catch //(Exception e) { oDs.Relations.Clear(); oDs.Tables.Clear(); //FillExceptionDataSet(oDs, e.ToString()); } return(r); }