使用日期时间选择器时出现错误信息 [英] Error message while using datetime picker

查看:124
本文介绍了使用日期时间选择器时出现错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我开发了一个带有日期时间选择器的小型Windows窗体应用程序.当我单击一个按钮时,datetime选择器中的日期将存储到sql数据库中.但是会存储从2012年1月2日到2012年12月12日的日期.但是,当输入13/02/2012时会显示错误
将varchar数据类型转换为日期时间数据类型会导致超出范围的值".

Hello,

I developed a small windows form application with a datetime picker. When i click a button, the date from datetime picker gets stored to sql database. But dates from 01/02/2012 till 12/02/2012 gets stored. But, when 13/02/2012 is entered an error is shown
"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value".

推荐答案

您需要检查日期时间格式(可以基于您或服务器的区域设置).
日期从01到12可能也会被错误地存储.月将被存储为前两个字符,而不是中间的字符.
You need to check your date time format (could be based on your or the server''s regional settings).
Dates from 01 to 12 may be getting stored incorrect as well. Month is getting stored as the first two characters and not the middle ones as would be expected.


不要像现在一样将DateTime转换为字符串.使用参数化查询,然后直接传递DateTime.这样,您不必担心SQL Server上的本地化日期格式(当前在PC上为dd/MM/yyyy)与ISO(yyyy-MM-dd)

Don''t convert the DateTime to a string, like you are doing at the moment. Use a Parametrized query, and pass the DateTime directly. That way, you do not have to worry about localised date format (currently dd/MM/yyyy on your PC) versus ISO (yyyy-MM-dd) on SQL Server

SqlCommand cmd = new SqlCommand("INSERT INTO myTable (dateColumn) VALUES (@DT)", con);
cmd.Parameters.AddWithValue("@DT", myDateTimePicker.Value);


这篇关于使用日期时间选择器时出现错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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