There is a variable DateTime ^ date, you need to enter its value into the MYSQL database in the format yyyy-MM-dd HH: ss: mm, how to convert the value to this format? C ++ cli

INSERT INTO bank.transactions (id_transaction,send_date) VALUES ( null, '"+date+"')",conDataBaseForTransaction); 

enter image description here

  • And what do you use for robots with a base? - Yaroslav
  • No need to enter the date-time as a string. Most DBMSs have a datetime type or something like that - and use it. - Alexander Petrov
  • Show the table creation script. | For God's sake, use the parameters . - Alexander Petrov
  • For God's sake, I already use datetime, the table has already been created - there is no script - Vlad Nikitin
  • Show how the table is created: CREATE TABLE , etc. | If the database uses the datetime type, then why do you need to format the string ? - Alexander Petrov

1 answer 1

When you write

 "INSERT ... VALUES (null, '" + date + "')" 

then the date variable automatically calls the ToString() method. That is, it is equivalent to the following:

 "INSERT ... VALUES (null, '" + date.ToString() + "')" 

When this type of DateTime formatted in accordance with the settings of the current culture. That is, if the Russian language is installed on the user's computer, the output format will be one, if English is the format will be different.

The DateTime.ToString method has several overloads. Among them is the host format string. We use it.

Set the format manually :

 "INSERT ... VALUES (null, '" + date.ToString("yyyy-MM-dd HH:ss:mm") + "')"; 

There are also standard formats. For example:

 date.ToString("s") // 2018-10-27T17:28:20 date.ToString("u") // 2018-10-27 17:28:50Z 

I do not know whether such formats MySql parse.


And now the main thing.

Never construct a sql query by gluing strings together. This is fraught with sql-injection (and disgraceful dismissal with a trademark unfit).

In addition, such queries are inefficient, since each time they are compiled, because a query with a new date is different from a query with a previous date. Meanwhile, the request with the parameters is cached , because the differences in the values ​​of the parameters are not taken into account.

Always use parameterized queries !

In the end, it should look something like this:

 String ^query = L"INSERT INTO bank.transactions(id_transaction, send_date) VALUES (@id, @date)"; MySqlCommand ^command = gcnew MySqlCommand(query, connection); command->Parameters->Add(gcnew MySqlParameter("id", null)); MySqlParameter ^dateParam = gcnew MySqlParameter("date", MySqlType::DateTime); dateParam->Value = date; command->Parameters->Add(dateParam); 

There may be errors in the syntax, I didn’t check it (there’s no muscle on hand and there’s no reluctance to put a provider). In which case, look for examples of using MySqlParameter .

  • Thank you very much: D This is a coursework so there is no time to bother with the validity of the code, especially in c ++ cli :), I will correct the requests. Have a nice day! - Vlad Nikitin