如何在格式13:38中将时间仅保存到MysQl数据库HH:mm [英] How to Save Time only to MysQl Database in Format 13:38 HH:mm

查看:64
本文介绍了如何在格式13:38中将时间仅保存到MysQl数据库HH:mm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以HH格式将时间仅保存到MySql数据库:mm例如13:38

我的代码在这里...

i want to save time only to MySql database in format HH:mm eg 13:38
My Code Goes Here..

string strInsert = "Insert into call_log(pno, name, complain_date, complain_time, problem, pcno, complain_no) values('123','name','" + DateTime.Now.ToString("yyyy/MM/dd") + "','" + DateTime.Now.ToString("HH:mm") + "','" + txtProblems.Text + "','" + txtPCno.Text + "','" + callID + "')";
                int effect = GlobalConectionClass.ExecuteNonQuery(strInsert);





它给出错误为---不正确的日期时间值:'13:38'用于第1行的'抱怨'时间'



我该怎么做plz帮助.. ..

在此先感谢!



it gives Error as--- Incorrect datetime value: '13:38' for column 'complain_time' at row 1

How Can i do it plz Help....
Thanks in Advance!

推荐答案

MySQL中没有数据格式可以节省时间:它必须有一个日期附件是为了有用。



我这样做的方法是将它存储为整数的午夜分钟,并将其转换回HH:当我提取它时MM。这样,您可以进行排序和比较等,并且仍然可以轻松检索显示值。





请不要连接用于构建SQL命令的字符串。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
There is no data format in MySQL which will save just the Time: it must have a date attached in order to be useful.

The way I would do this is to store it as "minutes since midnight" in an integer, and convert it back to HH:MM when I extracted it. That way, you can do sorting and comparisons etc. and still easily retrieve the values for display.


And please, 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.


如果您使用DataTypeDATETIME作为投诉时间字段,我认为您不能只存储时间。相反,您将DataType更改为TIME for Complain Time字段,并使用以下格式单独提取时间。

不要单独传递时间字符串并将日期作为参数传递,并使用这样的时间提取时间。 br />


插入测试时间值(2,TIME('2011-12-31 01:02:03'));



这只需要从给定日期起的时间 - 01:02:03。
If you are using DataType "DATETIME" for Complain Time field, I dont think you can store only the time. Instead you change the DataType into TIME for Complain Time field and use the below format to extract time alone.
Do not pass time string alone and pass the date as Parameter and extract the time using like this.

insert into testtime values(2,TIME('2011-12-31 01:02:03'));

this will take only the time from the given date - "01:02:03".


这篇关于如何在格式13:38中将时间仅保存到MysQl数据库HH:mm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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