Format in datetime C # to insert in datetime MYSQL column

I have a code like this:

AutoParkDataDataContext Db = new AutoParkDataDataContext(); Dailyreport dailyRep = new Dailyreport(); string time = Convert.ToDateTime("10-10-2014 15:00:00"); dailyRep.order_time = time; Db.Dailyreports.InsertOnSubmit(dailyRep); Db.SubmitChanges(); 

When I see it in the DailyReport table, it shows me only the date ("10-10-2014 00:00:00:00") , so the time is ignored. How can i fix this? The column type is DateTime .

+6
source share
4 answers

A quick / easy way to insert a date or date and time in MySQL is to use the format 'yyyy-MM-dd' or datetime like 'yyyy-MM-dd H: mm: ss'.

Try this

 DateTime theDate = DateTime.Now; theDate.ToString("yyyy-MM-dd H:mm:ss"); 

Make your SQL look like this.

 insert into mytable (date_time_field) value ('2013-09-09 03:44:00'); 
+20
source

Your line:

 string time = Convert.ToDateTime("10-10-2014 15:00:00"); 

Should not be compiled.

I can only assume that you do not have a DateTime as a column type in SQL Server, you should change it to save a DateTime , and then pass an object of type DateTime, not a string.

+2
source

This means that the underlying data type in the database must be Date . Change this to DateTime and it will also save time.

0
source
 DateTime dateTimeVariable = DateTime.Now; string date = dateTimeVariable.ToString("yyyy-MM-dd H:mm:ss"); 

The insert statement will look something like this:

 string InsertQuery = "INSERT INTO table( 'fieldName' ) VALUES ( '" + date + "' )"; 
0
source

Source: https://habr.com/ru/post/1501352/


All Articles