需要帮助解决SQLDateTime溢出错误

时间:2011-04-17 06:52:57

标签: asp.net

任何人都可以帮助解决此错误吗? SqlDateTime溢出。必须在1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间。

这是我的代码:

 <DataObjectMethod(DataObjectMethodType.Update)> _
    Public Shared Sub UpdateIncident( _
                                    ByVal DateClosed As DateTime, _
                                    ByVal Description As String, _
                                    ByVal original_IncidentID As Integer, _
                                    ByVal original_ProductCode As String, _
                                    ByVal original_DateOpened As DateTime, _
                                    ByVal original_DateClosed As DateTime, _
                                    ByVal original_Title As String, _
                                    ByVal original_Description As String, _
                                    ByVal IncidentID As Integer, _
                                    ByVal ProductCode As String, _
                                    ByVal DateOpened As DateTime, _
                                    ByVal Title As String)
        Dim con As New SqlConnection(GetConnectionString)
        Dim up As String = "UPDATE Incidents " _
           & "SET DateClosed = @DateClosed, " _
           & "Description = @Description" _
           & "WHERE IncidentID = @original_IncidentID " _
           & "AND ProductCode = @original_ProductCode " _
           & "AND DateOpened = @original_DateOpened " _
           & "AND (DateClosed = @original_DateClosed " _
           & "OR DateClosed IS NULL " _
           & "AND @original_DateClosed IS NULL) " _
           & "AND Title = @original_Title " _
           & "AND Description = @original_Description"
        Dim cmd As New SqlCommand(up, con)
        If DateClosed = #12:00:00 AM# Then
            cmd.Parameters.AddWithValue("DateClosed", DBNull.Value)
        Else
            cmd.Parameters.AddWithValue("DateClosed", DateClosed)
            cmd.Parameters("DateClosed").DbType = DbType.DateTime
        End If
        cmd.Parameters.AddWithValue("Description", Description)
        cmd.Parameters.AddWithValue("original_IncidentID", original_IncidentID)
        cmd.Parameters.AddWithValue("original_ProductCode", original_ProductCode)
        cmd.Parameters.AddWithValue("original_DateOpened", original_DateOpened)
        cmd.Parameters("original_DateOpened").DbType = DbType.DateTime
        If original_DateClosed = #12:00:00 AM# Then
            cmd.Parameters.AddWithValue("original_DateClosed", original_DateClosed)
        Else
            cmd.Parameters.AddWithValue("original_DateClosed", original_DateClosed)
            cmd.Parameters("original_DateClosed").DbType = DbType.DateTime
        End If
        cmd.Parameters.AddWithValue("original_Title", original_Title)
        cmd.Parameters.AddWithValue("original_Description", original_Description)
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    End Sub

2 个答案:

答案 0 :(得分:0)

当您有一个尚未设置的DateTime时会发生这种情况。 .NET DateTime.MinValue为'01 / 01/0001 00:00',SqlDateTime最小值为1753值。只需确保在将数据发送到数据库之前不会保留任何DateTime。

更新:

在您定义的方法中,添加一些逻辑来查找并修复传递给您的DateTime.MinValue。请接受我的C#,

if (theDateTime < SqlDateTime.MinValue)
{
    theDateTime = SqlDateTime.MinValue;
}

答案 1 :(得分:0)

SQL Server datetime数据类型的范围是:1753年1月1日到9999年12月31日

.NET DateTime数据类型的范围:0001年1月1日午夜12:00:00 Anno Domini(Common Era)到9999年12月31日晚上11:59:59 A.D

您可以使用SQL Server 2008的新数据类型:datetime2