private bool EliminarInformacion(DataTable dt, string sNombreTabla)
        {
            SqlCeParameter[] pars  = new SqlCeParameter[0];
            bool             error = false;
            string           sSentenciaParteInicial = string.Empty;
            string           sSentencia             = string.Empty;
            string           sValor = string.Empty;

            sSentenciaParteInicial = "delete from " + sNombreTabla + " where ";

            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    sSentencia = sSentenciaParteInicial;
                    foreach (DataColumn dc in dt.Columns)
                    {
                        sSentencia += dc.ColumnName + " = ";

                        if (System.Convert.IsDBNull(dr[dc]))
                        {
                            sValor      = "null";
                            sSentencia += sValor;
                        }
                        else
                        {
                            sValor = Convert.ToString(dr[dc]);
                            if (dc.DataType == Type.GetType("System.String") || dc.DataType == Type.GetType("System.DateTime"))
                            {
                                sSentencia += "'" + sValor + "'";
                            }
                            else
                            {
                                sSentencia += sValor;
                            }
                        }
                        sSentencia += " and ";
                    }
                    sSentencia = sSentencia.Substring(0, sSentencia.Length - 5) + ";";

                    DA.ConfigDataAccess.ObtenerConexion().EjecutarConsulta(sSentencia, pars);
                }
                catch (Exception e)
                {
                    HHsvc.SCPP_HH scpp = new HHsvc.SCPP_HH();
                    scpp.InsertaError(sSentencia, e.Message);
                    error = true;
                }
            }
            return(error);
        }
        public Boolean ActualizarDatosCatalogos(String tableName, int iCodigoPlanta, int iCodigoProceso)
        {
            HHsvc.SCPP_HH proxy  = DA.ConfigDataAccess.ObtenerServiceProxy();
            string        sError = string.Empty;
            Boolean       error  = false;

            try
            {
                List <DataSet> lds = new List <DataSet>();

                // Obtener fecha de ultima actualizacion de catalogos.
                DateTime dtFechaUltAct = this.co.ObtenerFechaUltimaActualizacion(0, 0);

                DateTime dtFechaDepuracionHistoria = co.ObtenerFechaDepuracionHistoria(iCodigoPlanta, iCodigoProceso);
                co.BorrarInfoTablaTransaccional(tableName, dtFechaDepuracionHistoria);
                DateTime dtFechaActual = DateTime.Now;
                DataSet  ds            = null;
                try
                {
                    // Obtener datos del servicio WCF.
                    ds = proxy.SyncServHH(tableName,
                                          dtFechaUltAct, true,
                                          dtFechaUltAct, true,
                                          dtFechaUltAct, true);
                }
                catch (Exception e)
                {
                    throw e;
                }
                lds.Add(ds);
                // Actualizar los datos locales.
                error = this.CargarDatosALocal(lds);
            }
            catch (Exception ex)
            {
                error = true;
                proxy.InsertaError("Table:tableName", ex.Message);
            }
            return(error);
        }
        public bool ActualizarInformacion(DataTable dtUpd, String tableName)
        {
            if (tableName.ToLower() == "usuario")
            {
                EncriptarContrasenaUsuario(ref dtUpd);
            }
            SqlCeParameter[] pars  = new SqlCeParameter[0];
            bool             error = false;

            String[] columnName = new String[dtUpd.Columns.Count];
            String   qry        = "";

            for (int i = 0; i < columnName.Length; i++)
            {
                columnName[i] = dtUpd.Columns[i].ColumnName;
            }

            foreach (DataRow dr in dtUpd.Rows)
            {
                try
                {
                    qry = "update " + tableName + " set ";
                    for (int i = 1; i < columnName.Length; i++)
                    {
                        qry += (i == 1 ? "" : ", ") + columnName[i] + " = " + columnValue(dr[i].GetType(), dr[i].ToString());
                    }
                    String pk = columnName[0].Trim().Length > 0 ? columnName[0] + " = " : "";
                    qry += " where " + pk + dr[0].ToString();

                    DA.ConfigDataAccess.ObtenerConexion().EjecutarConsulta(qry, pars);
                }
                catch (Exception e)
                {
                    HHsvc.SCPP_HH scpp = new HHsvc.SCPP_HH();
                    scpp.InsertaError(qry, e.Message);
                    error = true;
                }
            }
            return(error);
        }
        public bool ActualizarTablasCatalogos(String tableName, int planta, int proceso)
        {
            bool bError = true;

            try
            {
                List <DataSet> lds = new List <DataSet>();

                // Obtener fecha de ultima actualizacion de catalogos.
                DateTime dtFechaUltAct = this.co.ObtenerFechaUltimaActualizacion(0, proceso);


                DataSet ds = null;
                try
                {
                    // Obtener datos del servicio WCF.
                    HHsvc.SCPP_HH proxy = DA.ConfigDataAccess.ObtenerServiceProxy();
                    ds = proxy.ActualizarCatalogos(tableName,
                                                   planta, true,
                                                   proceso, true,
                                                   dtFechaUltAct, true);
                }
                catch
                {
                    throw new Exception("Error de Conexion de red");
                }
                lds.Add(ds);
                // Actualizar los datos locales.
                bError = this.CargarDatosALocal(lds);
            }
            catch (Exception ex)
            {
                bError = false;
                HHsvc.SCPP_HH proxy = DA.ConfigDataAccess.ObtenerServiceProxy();
                proxy.InsertaError("Table:tableName", ex.Message);
            }
            return(bError);
        }
        public bool InsertarInformacion(DataTable dt, string sNombreTabla)
        {
            SqlCeParameter[] pars  = new SqlCeParameter[0];
            bool             error = false;
            string           sSentenciaParteInicial = string.Empty;
            string           sSentencia             = string.Empty;
            string           sValor = string.Empty;

            try
            {
                if (sNombreTabla.ToLower() == "usuario")
                {
                    EncriptarContrasenaUsuario(ref dt);
                }
                sSentenciaParteInicial = "insert " + sNombreTabla + " (";
                foreach (DataColumn dc in dt.Columns)
                {
                    sSentenciaParteInicial += dc.ColumnName + ", ";
                }
                sSentenciaParteInicial = sSentenciaParteInicial.Substring(0, sSentenciaParteInicial.Length - 2) + ") values (";

                foreach (DataRow dr in dt.Rows)
                {
                    try
                    {
                        sSentencia = sSentenciaParteInicial;
                        foreach (DataColumn dc in dt.Columns)
                        {
                            if (System.Convert.IsDBNull(dr[dc]))
                            {
                                sValor      = "null";
                                sSentencia += sValor;
                            }
                            else
                            {
                                sValor = Convert.ToString(dr[dc]);
                                sValor = sValor.Replace("'", "");
                                if (dc.DataType == Type.GetType("System.String"))
                                {
                                    sSentencia += "'" + sValor + "'";
                                }
                                else if (dc.DataType == Type.GetType("System.DateTime"))
                                {
                                    DateTime d = Convert.ToDateTime(dr[dc]);
                                    sSentencia += "'" + d.ToString("yyyyMMdd HH:mm:ss") + "'";
                                }
                                else if (dc.DataType == Type.GetType("System.Boolean"))
                                {
                                    if (sValor.ToUpper() == "TRUE")
                                    {
                                        sSentencia += "1";
                                    }
                                    else if (sValor.ToUpper() == "FALSE")
                                    {
                                        sSentencia += "0";
                                    }
                                }
                                else
                                {
                                    sSentencia += sValor;
                                }
                            }
                            sSentencia += ", ";
                        }
                        sSentencia = sSentencia.Substring(0, sSentencia.Length - 2) + ");";

                        DA.ConfigDataAccess.ObtenerConexion().EjecutarConsulta(sSentencia, pars);
                    }
                    catch (Exception e)
                    {
                        if (sSentencia.IndexOf(".pk_") != -1)
                        {
                            HHsvc.SCPP_HH scpp = new HHsvc.SCPP_HH();
                            scpp.InsertaError(sSentencia, e.Message);
                            error = true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(error);
        }
        public bool ActualizarTablasTransaccionales(String tableName, int planta, int proceso)
        {
            bool bError = true;

            try
            {
                List <DataSet> lds = new List <DataSet>();

                // Obtener fecha de ultima actualizacion de catalogos.
                DateTime dtFechaUltAct = this.co.ObtenerFechaUltimaActualizacion(proceso, 0);

                DataSet ds  = null;
                DataSet ds2 = null;
                proceso = proceso == 1 ? 1 : co.ObtenerProcesoAnterior(proceso);
                DateTime dtFechaDepuracionHistoria = co.ObtenerFechaDepuracionHistoria(planta, proceso);
                co.BorrarInfoTablaTransaccional(tableName, dtFechaDepuracionHistoria);
                co.BorrarInfoTablaTransaccional("pieza_transaccion", dtFechaDepuracionHistoria);
                co.BorrarInfoTablaTransaccional("config_handheld", dtFechaDepuracionHistoria);
                co.BorrarInfoTablaTransaccional("pieza_reemplazo", dtFechaDepuracionHistoria);
                try
                {
                    // Obtener datos del servicio WCF.
                    HHsvc.SCPP_HH proxy = DA.ConfigDataAccess.ObtenerServiceProxy();
                    ds = proxy.ActualizarCatalogos(tableName,
                                                   planta, true,
                                                   proceso, true,
                                                   dtFechaUltAct, true);

                    //Extraer pendiente
                    if (proceso == 1 && tableName == "pieza")
                    {
                        try
                        {
                            DataTable dt = co.ObtenerPendientesRevision(tableName);
                            if (dt != null && dt.Rows.Count > 0)
                            {
                                ds2 = proxy.ActualizarCatalogosPorPiezas(dt, tableName, proceso, true);

                                if (ds2 != null && ds2.Tables.Count > 0)
                                {
                                    string sPrefijo  = "";
                                    string sNomTabla = "";
                                    foreach (DataTable dt2 in ds2.Tables)
                                    {
                                        sPrefijo  = dt2.TableName.Substring(0, 3).ToUpper();
                                        sNomTabla = dt2.TableName.Substring(3);

                                        if (sPrefijo == "UPD")
                                        {
                                            this.ActualizarInformacion(dt2, sNomTabla);
                                        }
                                    }
                                }
                            }
                        }
                        catch { }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error de Conexion de red");
                }
                lds.Add(ds);

                // Actualizar los datos locales.
                bError = this.CargarDatosALocal(lds);
            }
            catch (Exception ex)
            {
                bError = false;
                HHsvc.SCPP_HH proxy = DA.ConfigDataAccess.ObtenerServiceProxy();
                proxy.InsertaError("Table:tableName", ex.Message);
            }
            return(bError);
        }
        private Boolean CargarDatosALocal(List <DataSet> lDS)
        {
            Boolean error     = false;
            string  sNomTabla = string.Empty;

            HHsvc.SCPP_HH scpp    = new HHsvc.SCPP_HH();
            String        qry     = "";
            DataTable     dtIns   = new DataTable();
            DataTable     dtUpd   = new DataTable();
            DataTable     dtDel   = new DataTable();
            string        sDesIns = string.Empty;
            string        sDesUpd = string.Empty;
            string        sDesDel = string.Empty;

            bool bErrorIns = false;
            bool bErrorUpd = false;
            bool bErrorDel = false;

            try
            {
                if (lDS == null)
                {
                    return(false);
                }
                for (int i = 0; i < lDS.Count; i++)
                {
                    if (lDS[i] != null)
                    {
                        continue;
                    }
                    lDS.Remove(lDS[i]);
                    i--;
                }
                foreach (DataSet ds in lDS)
                {
                    foreach (DataTable dt in ds.Tables)
                    {
                        string sPrefijo = dt.TableName.Substring(0, 3).ToUpper();
                        sNomTabla = dt.TableName.Substring(3);
                        if (sPrefijo == "INS")
                        {
                            dtIns   = dt;
                            sDesIns = sNomTabla;
                            //bErrorIns = this.ObtenerInsert(dt, sNomTabla);
                        }
                        else if (sPrefijo == "UPD")
                        {
                            dtUpd   = dt;
                            sDesUpd = sNomTabla;
                            //bErrorUpd = this.ObtenerUpdate(dt, sNomTabla);
                        }
                        else if (sPrefijo == "DEL")
                        {
                            dtDel   = dt;
                            sDesDel = sNomTabla;
                            //bErrorDel = this.ObtenerDelete(dt, sNomTabla);
                        }
                    }
                }
                bErrorDel = this.EliminarInformacion(dtDel, sDesDel);
                bErrorIns = this.InsertarInformacion(dtIns, sDesIns);
                bErrorUpd = this.ActualizarInformacion(dtUpd, sDesUpd);
                if (bErrorIns || bErrorUpd || bErrorDel)
                {
                    error = true;
                }
            }
            catch (Exception ex)
            {
                scpp.InsertaError("Table:" + sNomTabla + "; QRY:" + qry, ex.Message);
                error = true;
                throw new Exception(this.sClassName + ", CargarDatosALocal: " + ex.Message);
            }
            return(error);
        }
        private void btAceptar_Click(object sender, EventArgs e)
        {
            try
            {
                Button btObj = this.btAceptar;//(Button)sender;

                this.encabezado.Mensaje = "Recopilando informacion";

                btObj.Enabled           = false;
                this.btCancelar.Enabled = false;
                Refresh();

                HHsvc.SCPP_HH proxy = DA.ConfigDataAccess.ObtenerServiceProxy();
                //iCodPlanta = isProcess ? iCodPlanta : Convert.ToInt32(cbxPlanta.SelectedValue);
                //iCodProceso = isProcess ? iCodProceso : Convert.ToInt32(cbxProceso.SelectedValue);

                DataTable dtTablasActualizar = new DataTable();

                // Carga de tablas Por Proceso
                if (!this.esCatalogo)
                {
                    dtTablasActualizar = proxy.TablasProcesoHH(iCodPlanta, true,
                                                               1, true,
                                                               iCodPantalla, true);
                }
                else
                {
                    dtTablasActualizar = proxy.TablasProcesoHH(0, true,
                                                               0, true,
                                                               iCodPantalla, true);
                }

                int cantRows = 0;
                if (dtTablasActualizar != null)
                {
                    cantRows = dtTablasActualizar.Rows.Count;
                }
                int contTables = 1;

                pbrProcesando.Minimum = 0;
                pbrProcesando.Maximum = cantRows;

                Boolean errorInserciones = false;
                String  sRes             = String.Empty;
                //Carga de Informacion Para los Catalogos
                foreach (DataRow dr in dtTablasActualizar.Rows)
                {
                    try
                    {
                        String table = dr[0].ToString();
                        this.encabezado.Mensaje = "Actualizando tabla: " + table.Replace("_", " ") + " " + contTables + "/" + cantRows;
                        Refresh();
                        Boolean error = esCatalogo ? oDA.ActualizarTablasCatalogos(table, iCodPlanta, iCodProceso) :
                                        oDA.ActualizarTablasTransaccionales(table, iCodPlanta, iCodProceso);
                        pbrProcesando.Value = contTables++;
                        if (error)
                        {
                            errorInserciones = error;
                        }
                    }
                    catch (Exception er)
                    {
                        errorInserciones = true;
                        proxy.InsertaError("Tabla:" + dr[0].ToString(), er.Message);
                    }
                }


                // Actualizar fecha de ultima actualizacion de catalogos.
                //if (!errorInserciones)
                DateTime dtFecha = DateTime.Now;
                bool     bFecha  = false;
                proxy.ObtenerFechaServidor(out dtFecha, out bFecha);


                if (esCatalogo)
                {
                    this.oDA0.EstablecerFechaUltimaActualizacion(0, iCodProceso, dtFecha);
                }
                else
                {
                    this.oDA0.EstablecerFechaUltimaActualizacion(iCodProceso, 0, dtFecha);
                }
            }
            catch (Exception ex)
            {
                String m = iCodPantalla != -1 ? this.encabezado.Mensaje : "";
                this.encabezado.Mensaje = String.Empty;
                Refresh();
                this.btCancelar.Enabled = true;
                MessageBox.Show(ex.Message + "---" + m, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
            tSync.Enabled = false;
            tSync.Dispose();
            this.Close();
        }