简体   繁体   中英

How to save string value as Datetime in Mysql using c#

I have a column in my mysql database table with datatype as Datetime .Now i have a string in my c# code with value as 17/04/2014 .So my question is how can i insert this value into my datetime column .

Please help me..

The following example convert date string to datetime using ParseExact method.

if you can guarantee dates will always be in a given format then you can use ParseExact()

        string dateString, format;  
        DateTime result;
        CultureInfo provider = CultureInfo.InvariantCulture;
        format ="dd/MM/yyyy";
         dateString = "17/04/2014";
        result = DateTime.ParseExact(dateString, format, provider);

在此处输入图片说明

EDITED

Replace your command text with this

cmd.CommandText = "insert into processeddata_table values(STR_TO_DATE('" + calldate + "','%d-%m-%Y'),'" + calltime + "','" + source + "','" + dialedno + "','" + extension + "','" + trunk + "','" + duration + "','" + toc + "','" + cost + "','" + site + "','" + callstatus + "','" + location + "','" + incomingcallduration + "','" + transfercallduration + "','" + outgoingcallduration + "','" + ringingduration + "','" + confereneceduration + "','" + empid + "','" + Department + "')";

yon can do like this:

string date="17/04/2014";

DateTime dt = Convert.ToDateTime(date);

There are many scenarios & solutions relates to your question.

Based on input, there are two methods.

DateTime strDate = DateTime.Parse("17/04/2014");

// for specific format like in your case dd/MM/yyyy:

DateTime strDateWithFormat = DateTime.ParseExact(strDate, "dd/MM/yyyy",null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM