转换规范vb.net的字符值无效MS访问SQL Server导入

时间:2016-08-03 14:51:49

标签: vb.net sql-server-2008 tsql ms-access insert

我的程序中有以下INSERT查询(变量从Access数据库中分配了一个值,并被插入到新的SQL Server数据库中)。代码运行良好,Parameters.Add部分中的值都是它们应该是的,但当它到达行sql.ExecuteNonQuery()时,我得到了错误

  

强制转换规范的字符值无效。      在System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior,String method)      在System.Data.OleDb.OleDbCommand.ExecuteNonQuery()

        For Each dr As DataRow In getDataTable(sqlct, importsCon, Nothing).Rows

            Dim conID As Integer
            Dim custacc As String
            Dim datestart As Object
            Dim daterenew As Object
            Dim fee As Double
            Dim agree As String
            Dim details As String
            Dim datesent As Object
            Dim timesent As Object
            Dim remname As String
            Dim remmail As String

            conID = dr("ContractID")
            custacc = dbToString(dr("CustAcc"))
            datestart = dbToDate(dr("DateStart"))
            daterenew = dbToDate(dr("DateRenew"))
            fee = dr("Fee")
            agree = dbToString(dr("Agreement"))
            details = dbToString(dr("OtherDetails"))
            datesent = dbToDate(dr("DateSent"))
            timesent = dbToTime(dr("TimeSent"))
            remname = dbToString(dr("ReminderName"))
            remmail = dbToString(dr("ReminderEmail"))

            sql.CommandText = "INSERT INTO tblContracts(ContractID, CustAcc, DateStart, DateRenew, Fee, Agreement, OtherDetails, DateSent, TimeSent, ReminderName, ReminderEmail) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
            sql.Transaction = tr
            sql.Parameters.Clear()
            sql.Parameters.Add("@ContractID", OleDbType.Integer).Value = conID
            sql.Parameters.Add("@CustAcc", OleDbType.VarChar).Value = custacc
            sql.Parameters.Add("@DateStart", OleDbType.DBDate).Value = datestart
            sql.Parameters.Add("@DateRenew", OleDbType.DBDate).Value = daterenew
            sql.Parameters.Add("@Fee", OleDbType.Double).Value = fee
            sql.Parameters.Add("@Agreement", OleDbType.VarChar).Value = agree
            sql.Parameters.Add("@OtherDetails", OleDbType.VarChar).Value = details
            sql.Parameters.Add("@DateSent", OleDbType.DBDate).Value = datesent
            sql.Parameters.Add("@TimeSent", OleDbType.DBDate).Value = timesent
            sql.Parameters.Add("@ReminderName", OleDbType.VarChar).Value = remname
            sql.Parameters.Add("@ReminderEmail", OleDbType.VarChar).Value = remmail

            sql.ExecuteNonQuery()
            sql.Parameters.Clear()

        Next

我的代码有什么问题?所有数据类型都与数据库的数据类型匹配(对象是数据库中的日期时间,但它们最初是对象,因为它们首先通过另一个函数)

1 个答案:

答案 0 :(得分:0)

正如评论中发现的那样,问题出在sql.Parameters.Add("@DateSent", OleDbType.DBDate).Value = datesent

因为要导入的数据库在一个或多个记录的此字段中存储空值,所以在检查它是否为空的函数中,它将从null转换为12:00:00 AM (这个代码没有在问题中给出,所以我不确定为什么或如何。

现在,您无法将12:00:00 AM作为DBDate值插入,因为正如它在锡上所说,它需要日期格式值(yyyyMMdd或类似值)。

此问题的解决方案是简单地将上述行更改为

sql.Parameters.Add("@DateSent", OleDbType.Date).Value = datesent

即使未指定日期,使用OleDbType.Date也不会插入值,因为它可以使用此值,并为此方法插入默认日期,我相信1899-12-30。但无论哪种方式,这都将解决您的问题