插入时间值时出现SQL DateTime溢出错误

时间:2014-01-21 14:30:05

标签: vb.net sql-server-2008

我正在尝试从组合框中将特定时间插入我的数据库。如果我选择其中一个索引,那么它会给出一个等于它的值。

我有3个字段,我将在以后使用它们以及以下内容:

  • time varchar(30)
  • timestart datetime
  • timeend datetime

但是当我尝试将其插入到我的数据库中时,它会出现一个错误,指出SQL DateTime溢出。必须在1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间“

我在将“timestart”和“timeend”值插入数据库时​​遇到了麻烦。

这是我将值插入数据库的代码。

   Try
            comm.CommandText = "insert into Schedule(sched_id, timestart,timeend, room, day,session, course, section, subject, instructor,time) values (@field1,@field2,@field3,@field4,@field5,@field6,@field7,@field8,@field9,@field10,@field11)"
            comm.Connection = con
            con.Open()
            With comm
                .Parameters.AddWithValue("@field1", txtSched_ID.Text)
                .Parameters.AddWithValue("@field2", timebegin)
                .Parameters.AddWithValue("@field3", timeend)
                .Parameters.AddWithValue("@field4", txtRoom.Text)
                .Parameters.AddWithValue("@field5", cmbDay.Text)
                .Parameters.AddWithValue("@field6", cmbSession.Text)
                .Parameters.AddWithValue("@field7", txtCourse.Text)
                .Parameters.AddWithValue("@field8", txtSection.Text)
                .Parameters.AddWithValue("@field9", txtSubject.Text)
                .Parameters.AddWithValue("@field10", cmbInstructor.Text)
                .Parameters.AddWithValue("@field11", timestg)
            End With
            comm.ExecuteNonQuery()
            comm.Dispose()
            comm.Parameters.Clear()
            MsgBox("Records Successfully Saved")
            clear()
            con.Close()
            Me.fillListViewSchedule()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

这是我的代码,用于声明我的“timestart”和“timeend”的值

      Private Sub cmbTime_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbTime.SelectedIndexChanged


    If cmbTime.Text = "7:00 AM - 10:00 AM" Then
        timestg = "7:00 AM - 10:00 AM"
        timebegin = #7:00:00 AM#
        timeend = #10:00:00 AM#
    ElseIf cmbTime.Text = "10:00 AM - 1:00 PM" Then
        timestg = "10:00 AM - 1:00 PM"
        timebegin = #10:01:00 AM#
        timeend = #1:00:00 PM#
    ElseIf cmbTime.Text = "1:00 PM - 4:00 PM" Then
        timestg = "1:00 PM - 4:00 PM"
        timebegin = #1:01:00 PM#
        timeend = #4:00:00 PM#
    ElseIf cmbTime.Text = "4:00 PM - 7:00 PM" Then
        timestg = "4:00 PM - 7:00 PM"
        timebegin = #4:01:00 PM#
        timeend = #7:00:00 PM#
    ElseIf cmbTime.Text = "7:00 PM - 10:00 PM" Then
        timestg = "7:00 PM - 10:00 PM"
        timebegin = #7:01:00 PM#
        timeend = #10:00:00 PM#
    End If
End Sub 

我将其声明为页面顶部..

   Dim timebegin As DateTime
Dim timeend As DateTime
Dim timestg As String

0 个答案:

没有答案