10

I have 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 could i fix it? The column type is DateTime.

2
  • What data type is your DailyReport.order_time property? From the looks of your code it is a 'string' type. Maybe change the properties data type to DateTime? Commented Sep 9, 2013 at 19:23
  • As one answer (which maybe should have been a comment) notes, your code as it stands does not compile. Please give more precise code. Are time and order_time of type DateTime, or of type string? Commented Sep 9, 2013 at 19:57

4 Answers 4

27

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');
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah. It helped me. And saved lots of time.
Be sure to copy from code block not comment. I copied as 'yyyy-mm-dd hh:mm:ss' month was weird ;)
@jstadnicki That's because mm is minutes.
2

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.

1 Comment

I just tried, it doesn't. Can't implicitly convert DateTime to string
1

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.

8 Comments

@maccettura, it's a DateTime .NET data type, unfortunately that has no bearing on the underlying data type in the database.
His question clearly states "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 could i fix it? The column type is DateTime." MySql has a DateTime data type, does it not? Am I missing something from your comment?
@maccettura, if the OP is looking at the table and not seeing the time, then the OP is mistaking the data type of the column in .NET for the actual underlying data type. There is nothing wrong with the .NET code, so if the database was in fact a DateTime when looking at the table the OP would see the time.
He is inputting a time of 15:00:00 and it is not showing up in the database (it shows as 00:00:00). There is still a time in the database, it is just a loss of precision. Meaning the problem lies in the code (again a loss of precision, most likely from the string>datetime conversions).
@maccettura, not lying. Mistaken. And yes, the InsertOnSubmit code isn't doing anything, it's a .NET framework method, they aren't dropping precision of data types for the fun of it.
|
1
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 + "' )";

3 Comments

i don't understand why this got a down vote! Am i missing something ?
The main problem is that you posted a code-only answer without any explanation.
That is very harsh. The code really does not need much explanation - and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.