如何解决sqldatetime溢出错误 [英] how to solve the sqldatetime overflow error
问题描述
HI,
我收到错误
SqlDateTime溢出。必须在1之间/ 1/1753 00:00 12:00和12/31/9999 11:59:59 PM。
i有一个datetimepicker控件来使用参数从日期中提取数据。
如何解决这个问题。
I am getting the error
"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."
i have a datetimepicker control to reieve data from date using with parameter.
How can i solve this problem.
string query = "select txtDate,txtCustomerName,txtAddress from NewEntryTable where txtDate=@myDate";
SqlConnection conn = new SqlConnection(connstr);
SqlDataAdapter dataAdapter = new SqlDataAdapter(query, conn);
dataAdapter.SelectCommand.Parameters.Add("@myDate", new DateTime());
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
推荐答案
也许看看这个:
日期时间结构 [ ^ ]
在评论中你写的是
Maybe have a look at this:
DateTime Structure[^]
In your comment you write
dataAdapter.SelectCommand.Parameters.Add("@myDate", new DateTime(15-9-2012,20-9-2013));
等于
which equals to
dataAdapter.SelectCommand.Parameters.Add("@myDate", new DateTime(-2006,-2002));
(我甚至惊讶你成功编译该代码,因为DateTime不接受只有两个Int32参数的构造函数。)
简单查看DateTime St的MSDN页面结构将帮助您了解如何构建给定的DateTime。
(I'm even surprised you managed to compile that code since DateTime does not accept a constructor with only two Int32 parameters.)
A simple look at the MSDN page of DateTime Structure will help you understanding how to build a given DateTime.
dataAdapter.SelectCommand.Parameters.Add("@myDate", new DateTime());
你需要给你的 DateTime
参数一个值。
试试
Try
dataAdapter.SelectCommand.Parameters.AddWithValue("@myDate", DateTime.Now);
或将DateTime.Now替换为您实际想要包含的日期和时间值:
or replace the DateTime.Now with the date and time value you actually want to include:
dataAdapter.SelectCommand.Parameters.AddWithValue("@myDate", new DateTime(2013, 2, 21, 14, 00, 00));
这篇关于如何解决sqldatetime溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!