format string in datetime c# to insert in MYSQL datetime column

后端 未结 4 560
一向
一向 2020-12-16 03:27

I have code like this:

AutoParkDataDataContext Db = new AutoParkDataDataContext();
Dailyreport dailyRep = new Dailyreport();
string time = Convert.ToDateTime         


        
相关标签:
4条回答
  • 2020-12-16 03:34

    A quick/easy method to insert date or datetime into MySQL is to use the format 'yyyy-MM-dd', or datetime as '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');
    
    0 讨论(0)
  • 2020-12-16 03:34
    DateTime dateTimeVariable = DateTime.Now;
    string date = dateTimeVariable.ToString("yyyy-MM-dd H:mm:ss");
    

    The insert Statement will look kind of like this:

    string InsertQuery   = "INSERT INTO table( `fieldName` ) VALUES ( '" + date + "' )";
    
    0 讨论(0)
  • 2020-12-16 03:37

    Your line:

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

    Shouldn't compile.

    I can only guess that you don't have DateTime as type of your column in SQL Server, you should modify it to keep DateTime and then pass a DateTime type object, not a string.

    0 讨论(0)
  • 2020-12-16 03:38

    This means that the underlying data type in the database must be Date. Change that to DateTime and it will store the time as well.

    0 讨论(0)
提交回复
热议问题