ASP.NET 日历控件返回日期类型,但我需要将日期时间插入 SQL Server 2005
我正在使用 ASP 日历控件将日期时间值插入到字段中,作为插入 SQL Server 2005 数据库表的一部分。
当我将表单提交到服务器并尝试插入表时,出现以下错误:
ArgumentException:正在使用的 SQL Server 版本不支持数据类型“日期”。
看起来日历控件返回一个日期类型值。我怎样才能让日历控件返回日期时间值?
我现在知道SQL Server 2005不支持Date类型字段。
这是我的客户端代码,我在 FormView 中拥有日历控件:
<tr>
<td class="style4">
Date</td>
<td>
<asp:Calendar ID="Calendar1" runat="server" BackColor="White"
BorderColor="#3366CC" BorderWidth="1px" CellPadding="1"
DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#003399" Height="200px" SelectedDate='<%# Bind("EventDate") %>'
Width="220px">
<SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
<WeekendDayStyle BackColor="#CCCCFF" />
<TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
<OtherMonthDayStyle ForeColor="#999999" />
<NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
<DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
<TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px"
Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
</asp:Calendar>
</td>
</tr>
I am using a ASP Calendar control to insert a datetime value into a field to be part of an insert to a SQL Server 2005 db table.
I get the following error when i submit the form to server and try to insert into table:
ArgumentException: The version of SQL Server in use does not support datatype 'date'.
Seems like Calendar control returns a Date type value. How could i make the Calendar control return a DateTime value instead?
I know now that SQL Server 2005 does not support Date type fields.
Here is my client code where I have my Calendar control in a FormView:
<tr>
<td class="style4">
Date</td>
<td>
<asp:Calendar ID="Calendar1" runat="server" BackColor="White"
BorderColor="#3366CC" BorderWidth="1px" CellPadding="1"
DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#003399" Height="200px" SelectedDate='<%# Bind("EventDate") %>'
Width="220px">
<SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
<WeekendDayStyle BackColor="#CCCCFF" />
<TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
<OtherMonthDayStyle ForeColor="#999999" />
<NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
<DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
<TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px"
Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
</asp:Calendar>
</td>
</tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,您试图创建一个包含
DATE
类型列的表。事实上,只有 SQL Server 2008 支持它,2005 不支持。即使
Calendar.SelectedDate
返回System.DateTime
并且您会尝试将其插入到DATETIME
类型的列中(在 SQL Server 2005 上)就会插入成功。如果列的类型为
DATE
(在 SQL Server 2008 上),则日期将被截断并成功插入As far as I could understand, you trying to create a table with column of type
DATE
. Indeed, only SQL Server 2008 support it, 2005 - don't.Even if
Calendar.SelectedDate
returnSystem.DateTime
and you will try to insert it into a column of typeDATETIME
(on SQL Server 2005) it will be inserted successfully.If column will have type
DATE
(on SQL Server 2008) the date will be truncated and also inserted successfully这是我的代码:
我现在已经把日期变成了日期时间问题。我将 EventDate 的 DbType 设置为 Datetime。但是,我现在得到:
[SqlTypeException:SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。]
Here is my code:
I've now gotten past the date into datetime issue. I set the DbType for EventDate to Datetime. However, I now get:
[SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.]
不确定你为什么会收到这个?您说的是 ASP.NET 2.0 或更高版本吗?默认情况下,它使用 DateTime 对象;您始终可以通过执行以下操作来确保这一点:
并使用它传递给数据库...
这是否也是由于插入过程也有日期而不是日期时间变量所致?
编辑:如果未提供 DateTime,则计算结果为 1/1/0001,这对于 SQL Server 来说是一个问题;你必须确保它不小于 1/1/1753。这是一个痛苦...如果您可以将该字段设为空并使用日期时间?或可为空,这是一种解决方法,或者始终确保最短日期。
Not sure why you are getting this? Are you talking ASP.NET 2.0 or greater? By default, it uses a DateTime object; you could always ensure this by doing:
And use this to pass to the DB...
Could this also be due to an insert proc that has a date and not a datetime variable too?
EDIT: A DateTime evaluates to 1/1/0001 when not supplied, which is an issue for SQL Server; you have to ensure it isn't less than the 1/1/1753. That is a pain... if you can make the field null and use a DateTime? or Nullable, that is a workaround, or always ensure a minimum date.
我相信它不会返回正确的格式,因为它会切换月份和年份。日历返回、日期时间和日期时间2 之间存在差异。您可能想要执行 datetime2,因为它包含 .net 可以返回的任何可能的值,但您需要对此做足功课。
从日历中获取日期的解决方案有很多,但我更喜欢的解决方案如下:
此代码片段应生成如下所示的内容:
然后您必须添加 where
然后将其添加到 where 子句之后的命令文本中。 (就像我的应用程序一样,您可能希望在表中包含空白和空值)
我使用一种方法来生成通常根据各种页面项目(例如复选框和日历)编写为 cmdText 的 SQL 命令。
I belive it does not return the correct format because it switches around the months and years. There is a difference between the calender return, datetime, and datetime2. You may want to do datetime2 as it holds any possibly values the .net can return, but you need to do your homework on this.
There are many solutions to getting your date from the calender but the one I prefer is the following:
This code snippet should generate something that looks like this:
You must then add the where
And then add this to your command text after the where clause. (like my application you may want to include blank and null values in your table)
I use a method to generate the SQL command usually written as cmdText based on various on page items such as checkboxes and calenders.