简体   繁体   中英

C# Input string was not in correct format for MySQL DateTime

I looking through the various pieces of information about this, and I couldn't find anything about it after an hour of searching, so I've been forced to ask something specifically.

In my MySQL database, I have a series of tables with a Created_dt column, which is a DateTime(6) field. An example of data would be: 2015-06-19 11:52:07.000000

I can pull any column from any table in the database except for this one, for some reason. Whenever I make a connection string, and fill a datatable with the adapter, I get the error in the title. Code:

MySqlCommand cmd = new MySqlCommand("Select Created_dt from comptran_bulk", connection);
MySqlDataAdapter da = new MySqlDataAdapter(cmd as MySqlCommand);
DataTable dt = new DataTable();
da.Fill(dt); // This is where the exception is thrown, when data is loaded into the table.

This command will eventually be replaced with "Select * from view_Sales" , and so the solutions to cast it differently I don't think will work. Also, none of the values for Created_dt are Null, so Allow Zero DateTime=True also didn't work.

What would you suggest?

The answer was going in an old error ticket in 2012.

"[1 Jun 2012 13:39] Dan Berger This does not seem to be fixed in 6.4.5 - I still get the FormatException.

I can narrow the problem down even more if it helps:

MySqlDateTime Success = new MySqlDateTime("2012-01-01 12:00:00");

MySqlDateTime Failure = new MySqlDateTime("2012-01-01 12:00:00.123456"); // throws FormatException

My server is "MySQL 5.1.41-3ubuntu12.10", if that makes a difference."

When using this in C# .NET, the microseconds are the cause of the error. After modifying things to become a datetime(0) instead of datetime(6), things have started to work.

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