如何将日期时间格式转换为mysql日期格式? [英] How to convert date time formate to mysql date format?

查看:573
本文介绍了如何将日期时间格式转换为mysql日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,我在其中输入用户的日期输入。但我无法将该日期保存到我的数据库,因为它显示无效日期格式的错误。如何将asp.net(C#)的日期时间格式更改为mysql db格式,即yyyy-mm-dd?

I have a textbox in which i take date input from user. But I can't save that date to my db as it shows error that invalid date format. How can I change the datetime format of asp.net(C#) to mysql db format which is yyyy-mm-dd?

推荐答案

Convert.ToDateTime(txtDate.Text).ToString(yyyy-MM-dd)
Convert.ToDateTime(txtDate.Text).ToString("yyyy-MM-dd")


m @ dhu#s中显示的第一步是正确的:转换你得到的字符串aspxpage到DateTime值。但是,使用参数化查询将数据导入数据库!通过字符串连接,你可以打开它进行SQL注入攻击。

你知道这样的攻击是如何工作的吗?好吧,在耻辱的大厅里读一个帖子: http://www.codeproject.com/Feature /WeirdAndWonderful.aspx?msg=4335687#xx4335687xx [ ^ ](关于注射攻击的信息来得太晚了 - 并且:他们还没有修复它,你可以试试你的技能......)
The first step shown in m@dhu#s answer is correct: convert the string you get from the aspxpage to a DateTime value. But then, use a parameterized query to get the data into your database! By string concatenation, you would open it for SQL injection attacks.
Do you know how such an attack works? Well, read a thread in the hall of shame: http://www.codeproject.com/Feature/WeirdAndWonderful.aspx?msg=4335687#xx4335687xx[^] (the information on the injection attack comes late - and: they haven't fixed it yet, you can try your skills...)


解决方案01是正确的。但这样做的标准方法是使用DateTimePicker。您可以在ToolBox的通用控件下轻松找到它。要根据给定的格式格式化日期,请按照以下步骤进行格式化:



1.选择DateTimePicker

2.press F4查看属性窗口

3.将CustomFormat的属性更改为yyyy-MM-dd

4.将格式的属性更改为自定义





如果您尚未将相关数据库字段定义为日期时间字段,请不要忘记转换DateTimePicker的输出因为它要求。
The Solution 01 is correct. But the standard way of doing this is using a "DateTimePicker". You can easily find it under the "Common Controls" in ToolBox. To format the date according to the given format, follow the steps given below:

1.select the "DateTimePicker"
2.press "F4" to see the properties window
3.change the property of "CustomFormat" as yyyy-MM-dd
4.change the property of "Format" as Custom


If you have not defined the relevant database field as a datetime field, don't forget to cast the output of "DateTimePicker" as it demands.


这篇关于如何将日期时间格式转换为mysql日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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