[英]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.