save values in mysql zerofill table with c #

1
    1   SucursalPrimaria    int(3)      UNSIGNED ZEROFILL   

I have a table with consecutive zerofill so I can save the zeros to the left but when I capture that data in c # and I add 1 to it and I want to save the next one, I leave it in 0, that is to say I read the 000 and I want to save it the 001, then the 002 but it does not increase them. I have the following code:

    public void obtenerconsecutivo()
    {
        miconsecutivo = 0;
        try
        {
            using (var mysql = new mysqlconect())
            {
                mysql.conexion2();
                mysql.cadenasql = "select Consecutivo from consecutivo";
                mysql.comando = new MySqlCommand(mysql.cadenasql, 
              mysql.con);
                mysql.comando.ExecuteNonQuery();
                using (var lee = mysql.comando.ExecuteReader())
                {
                    if (lee.Read())
                    {

                        misucursal = 1;
                        miterminal = 1;
                        mitipo = 1;
                        capturaconsecutivo = lee["Consecutivo"].ToString();

                    }
                }
                mysql.rol();
                mysql.Dispose();

                miconsecutivo = Int32.Parse(capturaconsecutivo)+1;
                MessageBox.Show(miconsecutivo.ToString("D10"));

                using (var mysql2 = new mysqlconect())
                {
                    mysql2.conexion2();
                    mysql2.cadenasql = "INSERT INTO 
               'consecutivo'('Sucursal', 'Terminal', 'Tipo', 'Consecutivo') 
                VALUES ('@sucursal','@terminal','@tipo','@consecutivo')";
                    mysql2.comando = new MySqlCommand(mysql2.cadenasql, 
                  mysql2.con);



          mysql2.comando.Parameters.AddWithValue("@sucursal",misucursal);

          mysql2.comando.Parameters.AddWithValue("@terminal",miterminal);
                    mysql2.comando.Parameters.AddWithValue("@tipo",  
           mitipo);

       mysql2.comando.Parameters.AddWithValue("@consecutivo",miconsecutivo);
                    mysql2.comando.ExecuteNonQuery();
                    mysql2.rol();
                    mysql2.Dispose();


                }

                label1.Text =string.Concat(misucursal.ToString("D3") , 
                miterminal.ToString("D5") , mitipo.ToString("D2") , 
                 miconsecutivo.ToString("D10"));


            }
        }
        catch (MySqlException mys)
        {
            mensajes.mensaje2(mys);
        }
        catch (Exception e)
        {
            mensajes.mensaje1(e);
        }



         }
    
asked by Richard Víquez Pérez 08.10.2018 в 07:31
source

1 answer

1

The first thing is that in the getconsecutive method you do many things, you should encapsulate your methods according to the function you perform, eg:

getConsecutive method:

    private static int ObtenerConsecutivo()
    {
        Int32 newConsecutivo = 0;
        var cadenaSql = @"SELECT Consecutivo FROM consecutivo";


        using (var mysql = new mysqlconect())
        {

            mysql.comando = new MySqlCommand(cadenaSql, mysql.con);

            try
            {
                newConsecutivo = (Int32)mysql.comando.ExecuteScalar();
            }
            catch (MySqlException mys)
            {
                mensajes.mensaje2(mys);
            }
        }
        return (int)newConsecutivo;

    }

As you can see, in this method I only do one thing: get the consecutive number.

On the other hand, I suspect that the field in the table that manages your consecutive number should be an AUTO_INCREMENT field, so you avoid having to be adding one manually by code.

Following the example of your code, your insert method would look like this:

private void Insert(string nombre, string direccion, DateTime fechaNacimiento)
{
    mysql2.cadenasql = @"INSERT INTO 
                       'consecutivo'('Sucursal', 'Terminal', 'Tipo', 'Consecutivo') 
                        VALUES ('?sucursal','?terminal','?tipo','?consecutivo')";


    using (var mysql2 = new mysqlconect())
    {
        mysql2.conexion2();

        int newConsecutivo = ObtenerConsecutivo() + 1;

        mysql2.comando = new MySqlCommand(mysql2.cadenasql, mysql2.con);
        mysql2.comando.Parameters.AddWithValue("?sucursal", misucursal);

        mysql2.comando.Parameters.AddWithValue("?terminal", miterminal);
        mysql2.comando.Parameters.AddWithValue("?tipo",mitipo);

        mysql2.comando.Parameters.AddWithValue("?consecutivo", newConsecutivo);

        int rowsAffected = mysql2.comando.ExecuteNonQuery();

        if (rowsAffected > 0)
        {
            label1.Text = string.Concat(misucursal.ToString("D3"),
            miterminal.ToString("D5"), mitipo.ToString("D2"),
            miconsecutivo = newConsecutivo.ToString("D10"));
        }

        mysql2.rol();
        mysql2.Dispose();

    }
}
    
answered by 08.10.2018 в 08:44