Mysql insert data and time but is long number ?

faustf

Member
Joined
Oct 8, 2020
Messages
5
Programming Experience
1-3
hi guys , i want insert inside a DB mysql a string , with actual day in this format string DateTime4_DB = string.Format("{0:yyyy-MM-dd--HH-mm-ss-fff}",marketDataUpdate.Time);
it work good but when in look inside of table of db , with navicat i saw only this number 1723, why ???
the cel is a text utf8mb4
anyone hhave some ideaa ?? thaks at all
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,756
Location
Sydney, Australia
Programming Experience
10+
Why would you do such a thing? There is never a good reason to store dates/times as text in a database that has dedicated types. If the database is within your control, fix the schema and use the correct type for storing dates/times and then store the proper data. Any formatting should be done at the application level, after you retrieve the data from the database.
 

faustf

Member
Joined
Oct 8, 2020
Messages
5
Programming Experience
1-3
i set datetime and i modify a C# cod ein sthis mode DateTime4_DB = string.Format("{0:yyyy-MM-dd HH:mm:ss:fff}",marketDataUpdate.Time); , but now return me error , You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '12:17:27:632,4083.25,4083,1)' at line 1
therfore i had changed to text
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,756
Location
Sydney, Australia
Programming Experience
10+
DO NOT save text to the database! Save a .NET DateTime value. There is no formatting. It's just a number internally. The only time you format is when you get the data back from the database and want to display it.
 

faustf

Member
Joined
Oct 8, 2020
Messages
5
Programming Experience
1-3
i modify my script in this mode and work string now_date_marketlast = DateTime.Now.ToString("yyyyMMddHHmmss");
but when i insert ("yyyyMMddHHmmssffff"); not work for me is important have also hundredths of a second , why not insert ? thanks at all
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,756
Location
Sydney, Australia
Programming Experience
10+
Are you not understanding or are you just ignoring what I'm posting? I would like to know because, if it's the latter, I won't waste any more time. DO NOT convert your DateTime value to a string. Save the DateTime value directly. Format is then irrelevant. Set the data type for your column to whatever MySQL uses for dates/times - it may be datetime or similar but I don't use MySQL much so I'm not sure - and then save DateTime.Now directly to the database. If you are converting a DateTime to a string for any purpose other than display then you're doing it wrong.
 
Top Bottom