Parameters.AddWithValue失败

时间:2017-08-20 18:19:19

标签: .net vb.net system.data.sqlite sqlite-net

我得到" System.FormatException:输入格式错误。"第二次尝试时出错,而第一次尝试完全正常。

有人知道为什么会这样吗?

尝试1:

    Using nCmdIns1 As SQLite.SQLiteCommand = cnUser.CreateCommand
        With nCmdIns1
            .CommandText = "INSERT INTO images (oemimageguid,imagetitle,imagecategory,imagesize,imageblob256) VALUES (@1,@2,@3,@4,@5)"
            .Parameters.Add("@1", DbType.String).Value = uOEMImageGUID
            .Parameters.Add("@2", DbType.String).Value = uTitle
            .Parameters.Add("@3", DbType.Int32).Value = iCat
            .Parameters.Add("@4", DbType.Int32).Value = uImageSize
            .Parameters.Add("@5", DbType.Binary).Value = uBytes
            .ExecuteNonQuery()
        End With
    End Using

尝试2:

    Using nCmdIns2 As SQLite.SQLiteCommand = cnUser.CreateCommand
        With nCmdIns2
            .CommandText = "INSERT INTO images (oemimageguid,imagetitle,imagecategory,imagesize,imageblob256) VALUES (@1,@2,@3,@4,@5)"
            .Parameters.AddWithValue("@1", DbType.String).Value = uOEMImageGUID
            .Parameters.AddWithValue("@2", DbType.String).Value = uTitle
            .Parameters.AddWithValue("@3", DbType.Int32).Value = iCat
            .Parameters.AddWithValue("@4", DbType.Int32).Value = uImageSize
            .Parameters.AddWithValue("@5", DbType.Binary).Value = uBytes
            .ExecuteNonQuery()
        End With
    End Using

我试图通过逐个删除参数和值来隔离问题,但最后,即使使用这条稀疏线,我也得到了同样的错误:

    Using nCmdIns3 As SQLite.SQLiteCommand = cnUser.CreateCommand
        With nCmdIns3
            .CommandText = "INSERT INTO images (oemimageguid) VALUES (@1)"
            .Parameters.AddWithValue("@1", DbType.String).Value = uOEMImageGUID
            .ExecuteNonQuery()
        End With
    End Using

以下是尝试3的例外情况的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:4)

AddWithValue的第二个参数是Value本身,而不是类型

请参阅MSDN AddWithValue

在任何情况下都尝试始终使用第一种方法,因为您可以更好地控制参数的类型。

Can we stop using AddWithValue already?

Using nCmdIns2 As SQLite.SQLiteCommand = cnUser.CreateCommand
    With nCmdIns2
        .CommandText = "INSERT INTO images (oemimageguid,imagetitle,imagecategory,imagesize,imageblob256) VALUES (@1,@2,@3,@4,@5)"
        .Parameters.AddWithValue("@1", uOEMImageGUID)
        .Parameters.AddWithValue("@2", uTitle)
        .Parameters.AddWithValue("@3", iCat)
        .Parameters.AddWithValue("@4", uImageSize)
        .Parameters.AddWithValue("@5", uBytes)
        .ExecuteNonQuery()
    End With
End Using