mysql中的DATETIME数据类型 [英] DATETIME datatype in mysql

查看:506
本文介绍了mysql中的DATETIME数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:

日期时间值不正确:'  4/10/2016 5:50:48 AM'  '  LastLogin'在行 1  





查询:

  string  updateLastLogin =  更新mydb.user SET LastLogin =' + System.DateTime.Now +  '其中Id =' +会话[  UserId]。ToString()+  ';; 







是不是4/10/2016 5:50:48 AM这个值不被mysql中的DATETIME数据类型接受?



如果是,那么应该是什么我确实存储了这个值。

请帮忙。



我尝试了什么:



错误:

日期时间值不正确:' < span class =code-string> 4/10/2016 5:50:48 AM'  for  column '  LastLogin'在行 1  





查询:

  string  updateLastLogin =  更新mydb.user SET LastLogin =' + System.DateTime.Now +  '其中Id =' +会话[  UserId]。ToString()+  ';; 







是4/10 / 2016年5:50:48 AM这个值不被mysql中的DATETIME数据类型接受?



如果是,那么我应该怎么做才能存储这个值。

请帮忙。

解决方案

简单:不要这样做!

不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。

将DateTime值作为日期时间传递到C#代码中,(以及ID应该是它应该是什么,可能是整数)将解决问题,并将有助于保存你的数据库 - 以及让你的代码更具可读性。

然后浏览你的其余代码,并确保你在其他地方也这样做!


您需要将其转换为mysql日期时间格式 MySQL :: MySQL 5.7参考手册:: 11.3.1 DATE,DATETIME和TIMESTAMP类型 [ ^ ]

试试这个:

 DateTime.Now.ToString(  yyyy-MM-dd HH:mm:ss


为了扩展Griff的答案(如果我可能如此大胆),我做了很多国际数据整合离子工作。请为了每个人的理智,永远不要使用日期。



我的抵押贷款的一半已由此支付。



等等......我在说什么?让我做生意! :)

Error:

Incorrect datetime value: '4/10/2016 5:50:48 AM' for column 'LastLogin' at row 1



Query:

string updateLastLogin = "Update mydb.user SET LastLogin='" + System.DateTime.Now + "' where Id='" + Session["UserId"].ToString() + "';";




is it that 4/10/2016 5:50:48 AM this value is not accepted by DATETIME datatype in mysql?

if yes then what should I do to store this value.
please help.

What I have tried:

Error:

Incorrect datetime value: '4/10/2016 5:50:48 AM' for column 'LastLogin' at row 1



Query:

string updateLastLogin = "Update mydb.user SET LastLogin='" + System.DateTime.Now + "' where Id='" + Session["UserId"].ToString() + "';";




is it that 4/10/2016 5:50:48 AM this value is not accepted by DATETIME datatype in mysql?

if yes then what should I do to store this value.
please help.

解决方案

Simple: don't do that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Passing the DateTime value as a DateTime in your C# code, (and the ID as whatever it should be, probably integer) will get rid of the problem, and will help to preserve your database - as well as making your code more readable.
Then go through the rest of your code and make sure you do the same everywhere else as well!


You need to convert it to mysql datetime format MySQL :: MySQL 5.7 Reference Manual :: 11.3.1 The DATE, DATETIME, and TIMESTAMP Types[^]
try this:

DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") 


To expand on Griff's answer (if I may be so bold) I do a lot of international data integration work. Please, for the sanity of everyone, NEVER USE STRINGS FOR DATES.

Half my mortgage has been paid by that, alone.

Wait... What am I saying? Keep me in business! :)


这篇关于mysql中的DATETIME数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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