Parameters.AddWithValue 失败
Parameters.AddWithValue failing
我在第二次尝试时遇到 "System.FormatException: The input has the wrong format." 错误,而第一次尝试却运行良好。
有人明白为什么会这样吗?
尝试 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 的异常截图:
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
我在第二次尝试时遇到 "System.FormatException: The input has the wrong format." 错误,而第一次尝试却运行良好。
有人明白为什么会这样吗?
尝试 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 的异常截图:
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