Convert date format to Mysql

Asked

Viewed 866 times

0

I’m developing a software in C# and in this software I asked to inform the customer’s date of birth in the format dd/mm/aaaa, but in the database the date type is only accepted in the format aaaa-mm-dd, I wanted to know if there is any kind of conversion in the bank to get the format I want

  • Put on what you’re doing.

2 answers

4


Perhaps better than formatting the date, is to use parameters in the query, thus avoiding security holes of type 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

  • 3

    Yes, this is right.

  • But like, no conversion at all in Mysql itself ? like some CAST

  • The correct way to run queries is for them to be parameterized because this improves the performance of the database because it will cache and will not compile its sql command again, and the conversion is at the discretion of the driver and the language to be used to pass queries concatenating string is wrong.

0

You can make C# format to the value that Mysql wants.

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

This in the case of a vaiável of the 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);

Browser other questions tagged

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