UPDATE SQL in C# (Windows Forms)

Asked

Viewed 121 times

-1

I am trying to format a standard date as there are dates with different formats as print below.

inserir a descrição da imagem aqui

MM/dd/yyyy | dd/MM/yyyy | dd/MM/yyyy hh:MM:ss

I intend to change these dates to default as dd/MM/yyyy hh:MM:ss

I thought to pull the dates and then click button to update the formats of all dates.

inserir a descrição da imagem aqui

Only I have no idea how to do that, I freeze at the time of making code.

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");
            }
        }
    }
  • I don’t understand why for, what his motive is?

  • I did not understand why it is after the update, which also has where. I even think that column in the bank is a varchar.

  • True, I forgot to put Where. Exactly that this column is varchar.

  • @Barbetta the loop for will format every line you have in the bank. But I think I found a way, I will post my code that I did differently up there.

2 answers

0


I did Cultureinfo class that converts the time of PT and EN... In this 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();
                }
            }

-1

The correct 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");
            }
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.