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 .
CREATE TABLE, etc. | If the database uses the datetime type, then why do you need to format the string ? - Alexander Petrov