无法将参数值从TimeSpan转换为DateTime [英] Failed to convert parameter value from a TimeSpan to a DateTime
问题描述
我正在尝试从文本框字段值存储时间,但出现此异常错误,我在做什么错呢?谢谢!:
I am trying to store a time from my textbox field value but I got this exception error, what am I doing wrong? Thanks!:
Failed to convert parameter value from a TimeSpan to a DateTime.
传递文本框值:
.CamActiveDateFrom = CDate(uitxtCamDateStart.Text.Trim)
我的变量和属性:
Private _camActiveTimeFrom As Nullable(Of TimeSpan)
Public Property CamActiveTimeFrom() As Nullable(Of TimeSpan)
Get
Return Me._camActiveTimeFrom
End Get
Set(ByVal value As Nullable(Of TimeSpan))
Me._camActiveTimeFrom = value
End Set
End Property
参数:
AddInParameter(dbCommand,
"@ActiveTimeFrom", DbType.Time, 6, DBNull.Value)
If (.CamActiveTimeFrom).HasValue Then
dbCommand.Parameters("@ActiveTimeFrom").Value = .CamActiveTimeFrom
End If
推荐答案
DbType.Time
记录为:
代表SQL Server DateTime值的类型.如果要使用SQL Server时间值,请使用
SqlDbType.Time
.
所以或者都使用 SqlDbType.Time
,将或的值设置为 DateTime
的值,而不是 TimeSpan
值.
So either use SqlDbType.Time
instead, or set the value to a DateTime
value instead of a TimeSpan
value.
必须此映射表显示 DbType.Time
映射到 TimeSpan
,因此这里存在一些不一致之处-但是,如果您可以使用更具体的类型,那就太好了.
Admittedly this mapping table show the DbType.Time
mapping to TimeSpan
, so there's some inconsistency here - but if you can use a more specific type, that can only be good.
当然,如果愿意的话,可以很容易地从 TimeSpan
构造一个 DateTime
-您可能应该确定一个始终使用的特定日期(例如2000年1月1日)这样您就可以比较数据库中的值并有效地进行时间位的比较.
Of course it's easy to construct a DateTime
from a TimeSpan
if you want to - you should probably decide on a specific date to always use (e.g. 1st January 2000) so that you can then compare values within the database and effectively get a comparison of just the time bits.
这篇关于无法将参数值从TimeSpan转换为DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!