我在MySql中存储和检索正确的日期/时间时遇到问题 [英] I have a problem with storing and retrieving correct date/time in MySql

查看:78
本文介绍了我在MySql中存储和检索正确的日期/时间时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI
我已经在Mysql中创建了一个数据库,并且在其中一个表中有一个datetime字段.在我的C Sharp代码中,我创建一个DateTime对象,然后尝试保存在数据库字段中.日期和时间保存在数据库中,但是不正确.我有一个问题,当我将DateTime.Now.ToString()保存到我的数据库时出现错误,它说日期/时间格式不正确. Mysql中的日期分隔符是-",C#使用"/",因此我操纵了字符串,并用-"替换了"/",它似乎起作用了. 我的问题是,即使保存了PM时间,Mysql也会每次记录为AM.有人知道如何解决吗? :confused:

在此先感谢您的帮助!

HI
I have created a database in Mysql and i have a datetime field in one of my tables. In my C Sharp code i create a DateTime object and i try to save in my database field. The date and time saves in the database but it is incorrect. I had the problem where i was getting an error when i saved DateTime.Now.ToString() to my database, it said incorrect date/time format. The date seperator in Mysql was a "-" and C# used a "/", so i manipulated the string and replaced "/" with "-" and it seemed to worked. The problem i have is that Mysql records every time as AM even though a PM time is being saved in it. Does anyone know how to solve this? :confused:

Thanks in advance for you help!

推荐答案

Don;''不要使用字符串,请使用日期时间.然后就没有解析错误的空间了.
Don;''t use a string, use a datetime. Then there''s no room for parsing errors.


已经给出了正确的答案.无需在这些论坛上大喊大叫-当有人善良并愿意为您提供答案时,您会得到答案,而不是在之前.不要在SQL中使用字符串作为日期,而要使用日期.使用参数化查询,您将不会遇到此问题.如果您不知道参数化查询是什么,则应从基础知识入手.如果插入日期有问题,则在sql中将日期转换为日期SELECT Name FROM Table WHERE SalesDate = DATE(@SALESDATE).这在MySql中有效,在SQL Server或Access中可能无效.
The correct answer was already given. There is no need to shout on these forums - you will get an answer when someone is good and ready to give you an answer and not before. Do not use a string as a date in your sql, use a date. Use Parameterized queries and you will not have this problem. If you don''t know what parameterised queries are you should start with the basics. If there is a problem inserting dates then convert to date in your sql SELECT Name FROM Table WHERE SalesDate = DATE(@SALESDATE) This works in MySql, maybe not in SQL server or Access.


这篇关于我在MySql中存储和检索正确的日期/时间时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆