UPDATE SQL in C # (Windows Forms)

-1

I'm trying to format a default date because there are dates with different formats, as print below.

MM/dd/yyyy|dd/MM/yyyy|dd/MM/yyyyhh:MM:ss

Iwanttochangethesedatestodefaultasdd/mm/yyyyhh:MM:ss

Ithoughtaboutpullingthedatesandthenclickingbuttontoupdatetheformatsofalldates.

ButIdonothavetheideatodothis,it'stimetomakecode.

publicvoidAlteraData(DatanewData){try{stringquery=command.CommandText="'UPDATE T410_CARGA SET T400_DTCHEGADA = '" + newData.Data;
            for (int linha = 0; linha <= 10; linha++)
            {
                newData.ToString("dd/mm/yyyy hh:MM:ss");
            }
        }
    }
    
asked by anonymous 21.05.2018 / 20:44

2 answers

0

I made CultureInfo class that converts the PT and EN time ... In that case I converted from English to Portuguese.

public void AlteraData(DataTable newData)
        {
            CultureInfo cultura = new CultureInfo("pt-BR");
            for (int linha = 0; linha < newData.Rows.Count; linha++)
            {
                DateTime data1 = Convert.ToDateTime("01/01/1900");
                string teste = newData.Rows[linha]["T400_DTCHEGADA"].ToString();
                var separador = teste.Split('/');
                //Console.Write(separador[0].ToString());
                if (Convert.ToInt32(separador[0]) > 12 || Convert.ToInt32(separador[1]) > 12)
                {
                    try
                    {
                        data1 = Convert.ToDateTime(teste, cultura);
                    }
                    catch (Exception)
                    {
                        data1 = Convert.ToDateTime(teste);
                    }
                }
                else
                {
                    data1 = Convert.ToDateTime(teste, cultura); ;
                }
                var dataConvertida = data1.ToString("MM/dd/yyyy");

                if (dataConvertida != "01/01/1900")
                {
                    //Console.WriteLine(teste);

                    command.CommandText = "UPDATE T410_CARGA SET T400_DTCHEGADA = '" + dataConvertida + "' WHERE T400_DTCHEGADA = '" + teste + "'";
                    command.CommandType = CommandType.Text;
                    connection.Open(); 
                    command.ExecuteNonQuery();
                    connection.Close();
                }
            }
    
22.05.2018 / 15:25
-1

The correct one is: newData.ToString("dd/MM/yyyy HH:mm:ss");

public void AlteraData(Data newData)
    {
        try
        {
            string query = command.CommandText = "'UPDATE T410_CARGA SET T400_DTCHEGADA = '" + newData.Data;
            for (int linha = 0; linha <= 10; linha++)
            {
                newData.ToString("dd/MM/yyyy HH:mm:ss");
            }
        }
    }
    
21.05.2018 / 20:49