Getting DateTime from MySQL table using C# and AllowZeroDatetime -


i connecting mysql c# using allowzerodatetime option , when query table, works part of way. date zeros time 12:00:00 am. how fix this? here connection string:

string connectioninfo = @"server=" + @server + @";userid=" + @username + @";password=" + @password + @";database=my_data;" + @"allowzerodatetime=true;"; 

and here code i'm using datetime entries:

private static void getselectgroups(mysqlconnection mysqlconn) {      string selectcommand = "select user_date my_table";      // set table reader     mysqlcommand sqlcmd = new mysqlcommand(selectcommand, mysqlconn);     mysqldatareader dataread = sqlcmd.executereader();      while (dataread.read())     {         console.writeline(dataread["user_date"]);     } } 

this printing out 0/0/0000 12:00:00 am. need print 0/0/0000 00:00:00.

since dataread["user_date"] returns object, console.writeline(overload) implemented as;

if (value == null) {     writeline(); } else {     iformattable f = value iformattable;     if (f != null)         writeline(f.tostring(null, formatprovider));     else         writeline(value.tostring()); } 

since value not null , datetime implements iformattable interface, line executed;

writeline(f.tostring(null, formatprovider)); 

and performs the "g" standard format specifier of currentculture combinations of shortdatepattern , longtimepattern properties.

for current culture, looks combinations performs d/m/yyyy hh:mm:ss tt format.

for string representation want, either use custom date , time formats or can clone currentculture set it's longtimepattern property hh:mm:ss format need 00:00:00 representation , use culture.

console.writeline(((datetime)dataread["user_date"]).tostring("d/m/yyyy hh:mm:ss")); 

or

var clone = (cultureinfo)cultureinfo.currentculture.clone(); clone.datetimeformat.longtimepattern = "hh:mm:ss";  while (dataread.read()) {     console.writeline(((datetime)dataread["user_date"]).tostring(clone)); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -