将varchar数据类型转换为日期时间数据类型会导致超出范围的值 [英] The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

查看:113
本文介绍了将varchar数据类型转换为日期时间数据类型会导致超出范围的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当输入日期字段时最小日期如01/01/0001并单击搜索然后它显示错误消息将varchar数据类型转换为日期时间数据类型导致超出范围的值

when Date field is entered minimum date like 01/01/0001 and click on Search then it is displaying error message "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

推荐答案

您应该使用 TryParse 来查看是否可以解析用户输入。



You should use TryParse to see if it's possible to parse users input.

string datestring = "01/01/0001";
DateTime date;
if (DateTime.TryParse(datestring, out date))
{
    // Date parsed ok. All good.
}
else
{
    // Could not parse datestring or the date is out of range. 
    // Tell the user to try other input
}


它不是你的查询的问题。我想告诉你它是由DB。

Sql中允许的日期范围



1753年1月1日至12月31日, 9999



如果你使用数据类型作为smalldatetime那么



1900年1月1日到2079年6月6日



将日期置于其中您的查询将会运行。
Its not problem of your query. i want to tell you that it is by DB.
The date range allowed in Sql

January 1, 1753, through December 31, 9999

and if you using datatype as smalldatetime then

January 1, 1900 through June 6, 2079

Put date within this your query will run.


您好uday



string date =01/01/0001;

DateTime now = Convert.ToDateTime(date);
Hi uday

string date = "01/01/0001";
DateTime now = Convert.ToDateTime(date);


这篇关于将varchar数据类型转换为日期时间数据类型会导致超出范围的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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