Convert date format to MySql

0

I am developing software in C# and in this software I asked to inform the date of birth of the client in dd/mm/aaaa format, but only in the database the data type is only accepted in aaaa-mm-dd format, I wanted to know if there is any kind of conversion in the bank to stay in the format I want

    
asked by anonymous 25.04.2016 / 17:38

2 answers

4

Perhaps better than formatting the date, or using parameters in the query, thus avoiding SQLInjection

So, instead of concatenating the strings:

"UPDATE Clientes SET dataNascimento = " + minhaDataString

you can do:

"UPDATE Clientes SET dataNascimento = :dataNascimento"

Command.Parameters.Add("dataNascimento", minhaData, SqlDbType.DateTime)

Because regardless of the database date format used, you let the framework do its job and encapsulate this rule for you

    
25.04.2016 / 18:34
0

You can make C # format the value that MySQL wants.

MinhaData.ToString("yyyy-MM-dd")

This is in the case of a vaiable of type DateTime . If you only have the string "dd / mm / yyyy" you will have to convert to DateTime and then format.

string Data = "01/01/2016"  
DateTime MinhaData = DateTime.Parse(Data);
    
25.04.2016 / 18:11