获取错误:将字符串转换为smalldatetime数据类型时转换失败。再次查询DB时

时间:2014-11-07 15:21:06

标签: sql sql-server vb.net

尝试插入表格时出错。由于错误显示我知道是因为smalldatetime字段。但是我已经尝试了几个不同的东西从其他帖子,它不起作用。另外,当我从同一个表中读取时,我使用相同的格式进行smalldatetime并且它可以工作。代码如下所示。任何帮助将非常感激。

尝试插入新行时的代码 .vb.net

Dim conn As String = "Your Connection String"
    'Dim querystring As String = "INSERT INTO  'Where SupID='" & supervisor & "' Order By EmpNum"
    Dim querystring As String = "INSERT INTO [AttendanceDB].[dbo].[tblAbsences]([fldEmployeeID] " & _
                                ",[fldAbsentDate],[fldAbsentCode],[fldRuleViolationWarningType],[fldRuleViolationIssueDate] " & _
                                 ",[fldLOAEndDate]) VALUES('23','11-06-2014','NA','NULL','NULL','NULL')"

    Using connection As New SqlConnection(conn)
        Dim command As New SqlCommand(querystring, connection)
        connection.Open()
        Try
            command.ExecuteNonQuery()

        Catch ex As Exception
            connection.Close()
        End Try
    End Using

我也在SQL Management Studio中尝试过这段代码

INSERT INTO [AttendanceDB].[dbo].[tblAbsences]([fldEmployeeID],
        [fldAbsentDate],[fldAbsentCode],[fldRuleViolationWarningType],
        [fldRuleViolationIssueDate],[fldLOAEndDate])
VALUES     ('23','11-06-2014','NA','NULL','NULL','NULL')

这是在数据绑定到ViewGrid

时从同一个表中读取时有效的代码
Dim startdate As String = txtcalendarstart.Text
    Dim enddate As String = txtcalendarstop.Text
    startdate = startdate.Replace("/", "-")
    enddate = enddate.Replace("/", "-")
    Dim SqlDataSource2 As SqlDataSource = New SqlDataSource()
    SqlDataSource2.ID = "SqlDataSource2"
    Page.Controls.Add(SqlDataSource2)
    SqlDataSource2.ConnectionString = "Data Source=SVR-SQLDB2;Initial Catalog=AttendanceDB;Integrated Security=True"        
    SqlDataSource2.SelectCommand = "SELECT * FROM [tblAbsences] WHERE [fldAbsentDate] BETWEEN '7-03-2014' AND '8-21-2014' ORDER BY [fldEmployeeID]"        
    GridView1.DataSource = SqlDataSource2
    GridView1.DataBind()

0 个答案:

没有答案