简体   繁体   English

从文本框中获取输入并将其插入 mysql 数据库时,如何更正我的日期时间值?

[英]how do I correct my date time value when taking an input from a text box and inserting it into mysql database?

I keep getting this error from visual studio when I insert a date value into my database当我将日期值插入数据库时,我不断从 Visual Studio 收到此错误

MySql.Data.MySqlClient.MySqlException: 'Incorrect datetime value: '0014-04-2021'' MySql.Data.MySqlClient.MySqlException:'不正确的日期时间值:'0014-04-2021''

I changed my DateTime picker format to short so that it doesn't include the time when it takes the value from the DateTime picker but the problem still persists.我将我的 DateTime 选择器格式更改为 short,以便它不包括从 DateTime 选择器获取值的时间,但问题仍然存在。

this is my SQL query code这是我的 SQL 查询代码

MySqlCommand cmd = new MySqlCommand("insert into inventory.vehicle(regdate) values(@regdate)", conn);

cmd.Parameters.Add("@regdate", MySqlDbType.Date).Value = datereg.Text;

int i = cmd.ExecuteNonQuery();

You should have your column declared as a DATE type in MySQL您应该在 MySQL 中将您的列声明为 DATE 类型

Then use .Date on the datetimepicker's .Value然后在 datetimepicker 的.Value上使用.Date

cmd.Parameters.Add("@regdate", MySqlDbType.Date).Value = datereg.Value.Date;

You should note that this does not "remove" a time from a date;您应该注意,这不会从日期中“删除”时间; it sets it to midnight.它设置为午夜。 There is no way to remove a time from a date- a moment in time is a moment in time, just like 1 and 1.0 are the same thing.没有办法从日期中删除时间——时刻就是时刻,就像 1 和 1.0 一样。 1 might be formatted without any decimal digits but it's illogical to say that 1 and 1.0 are different 1 的格式可能没有任何十进制数字,但说 1 和 1.0 不同是不合逻辑的

Once you make all your dates have the same time (midnight, 00:00:00 which is effectively the.0 in 1.0) the ability to compare datetimes in date alone makes sense.一旦您使所有日期都具有相同的时间(午夜,00:00:00,这实际上是 1.0 中的.0),仅在日期中比较日期时间的能力就很有意义。

try to use simple <input type="date"> , because it has default format to YYYY-MM-DD尝试使用简单的<input type="date"> ,因为它的默认格式为 YYYY-MM-DD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从列表框中取出数据时,如何从C#中的字符串计算值? - How do I calculate a value from a string in C# when taking data out of a list box? 从C#应用程序文本框中将值插入MySQL数据库 - Inserting Values into MySQL database from C# application text box 如何将在TinyEditor框中输入的文本保存到数据库中 - How do I save text entered in a TinyEditor box to my database 选择路径名称时,如何从文本框中的组合框中获取选定的数据库名称 - How do I get selected database name from Combo box in a text box when I choose the Path name 如何多次从总文本框中减去折扣文本框? - How do i subtract discount text box from total text box more than one time? 当我单击图钉时,如何将正确的图像从数据库加载到正确的项目? - How to load the correct image from my database to the correct item when I click on my pin? 从两个文本框中插入日期和时间 - Inserting Date and time from two text boxes 在运行时创建多个文本框并从数据库分配值文本框。我想显示数据库中的所有数据 - create at run time multiple text box and assign value textbox from database..i want to show all the data from database 从数据库插入数据后,如何更改标签的值? - How do I change the value of a label after inserting a data from the Database? 将选定的值从Gridview插入到Mysql数据库 - Inserting Selected value from Gridview to Mysql database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM