首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Parameters.AddWithValue失败

Parameters.AddWithValue失败
EN

Stack Overflow用户
提问于 2017-08-21 02:19:19
回答 1查看 321关注 0票数 0

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

有没有人明白为什么会这样?

尝试1:

代码语言:javascript
复制
    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:

代码语言:javascript
复制
    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

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

代码语言:javascript
复制
    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的异常屏幕截图:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-21 02:24:04

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

请参阅MSDN AddWithValue

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

Can we stop using AddWithValue already?

代码语言:javascript
复制
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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45785446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档