Validate that a record does not duplicate from C #

0

Good day, I request your support since I have a watchdog on a local team with a BD MySQL , of which I want to insert the attendance records in a BD in SQL-Server , I already generate both connection chains and I am inserting the registers correctly, I am having some complications in the validation, so that the records are not duplicated, if I execute the different queries .

I enclose the methods that I am using to see if they can support me to detect where I am making the mistake.

Thanks for your help everyone.

    //Metodo para buscar todos los registros en el reloj local

    public void BuscarChecadasReloj()
    {
        try
        {
            dtChecadasReloj = objRelojChecador.RegistrosPersonal();
            if (dtChecadasReloj.Rows.Count != 0)
            {
                foreach (DataRow drChecadasReloj in dtChecadasReloj.Rows)
                {
                    CNumeroEmpleado = Convert.ToInt32(drChecadasReloj[0]);
                    CNombreEmpleado = Convert.ToString(drChecadasReloj[1]);
                    CTipoChecada = Convert.ToString(drChecadasReloj[2]);
                    CHoraChecada = Convert.ToString(drChecadasReloj[3]);
                    CFechaChecada = Convert.ToString(drChecadasReloj[4]);
                    CEstatus = "PENDIENTE";
                    for (int i = 0; i <= dtChecadasReloj.Rows.Count; i++)
                    {
                        BuscarChecadasIntelisis();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

        //Método para buscar registros en SQL-Server
        public void BuscarChecadasIntelisis()
        {
            try
            {
                if (dtChecadasReloj.Rows.Count!=0)
                {
                    dtChecadasIntelisis = objCosmetica.ChecadasPersonal(CNumeroEmpleado, CNombreEmpleado, CTipoChecada, CHoraChecada, CFechaChecada, CEstatus);
                }                
                if (dtChecadasIntelisis.Rows.Count != 0)
                {
                    foreach (DataRow drChecadasIntelisis in dtChecadasIntelisis.Rows)
                    {
                        INumeroEmpleado = Convert.ToInt32(drChecadasIntelisis[0]);
                        INombreEmpleado = Convert.ToString(drChecadasIntelisis[1]);
                        ITipoChecada = Convert.ToString(drChecadasIntelisis[2]);
                        IHoraChecada = Convert.ToString(drChecadasIntelisis[3]);
                        IFechaChecada = Convert.ToString(drChecadasIntelisis[4]);
                        IEstatus = Convert.ToString(drChecadasIntelisis[5]);
                        if (INumeroEmpleado != 0 && INombreEmpleado != "" && ITipoChecada != "" && IHoraChecada != "" && IFechaChecada != "" && IEstatus != "")
                        {
                            for (int i = 0; i <= dtChecadasReloj.Rows.Count; i++)
                            {
                                if (INumeroEmpleado == CNumeroEmpleado && INombreEmpleado == CNombreEmpleado && ITipoChecada == CTipoChecada && IHoraChecada == CHoraChecada && IFechaChecada == CFechaChecada && IEstatus == CEstatus)
                                {
                                    RegistradoIntelisis = true;
                                    ProcesarChecadas();
                                }
                                else
                                {
                                    GuardarRegistroIntelisis = true;
                                    ProcesarChecadas();
                                }
                            }
                        }
                        else
                        {
                            GuardarRegistroIntelisis = true;
                            ProcesarChecadas();
                        }
                    }
                }
                else
                {
                    GuardarRegistroIntelisis = true;
                    ProcesarChecadas();
                }
            }        
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

    //Método para insertar registros del reloj en SQL-Server
    public void ProcesarChecadas()
    {
        try
        {
            if (RegistradoIntelisis)
            {
                objValidaciones.MostrarAviso("Todos los registros se encuentran actualizados en la base de datos", true, lblAviso);
                BuscarChecadasIntelisis();
            }
            if (GuardarRegistroIntelisis)
            {
                objCosmetica.InsertarAsistencias(CNumeroEmpleado, CNombreEmpleado, CTipoChecada, CHoraChecada, CFechaChecada, CEstatus);
                RegistradoIntelisis = true;
                BuscarChecadasReloj();
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    
asked by Alberto Arenas 19.09.2017 в 17:50
source

1 answer

0

First of all I think that the validation should be in the database place your columns of date and time that are primary and it would be important that you show your bd scheme to know more or less which fields are indicated.

that is, create unique fields in your table where you register the entry the fields that to my concidenracion must be unique are Number Employed TypeChecada DateChecked

that will be enough to not double your registration in one day.

    
answered by 19.09.2017 в 18:59