图像上传时出现MySQL语法错误

时间:2013-06-04 16:37:58

标签: asp.net mysql

我在网站上有一个小文件上传,应该上传一个选定的图像并将其放在一个mysql数据库中。图像存储为varbinary类型(8000长度)。我的代码是:

 Public Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If fileupload1.HasFile Then

        Dim pimage As Byte() = fileupload1.FileBytes
        Dim pid As String = partnum.Text.ToString
        Dim sConnection As String = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=inteva; UID=root; PASSWORD=root; OPTION=3"
        Dim connectme As New OdbcConnection(sConnection)
        Dim sInsertInto As String = "INSERT INTO images(serial, image1) VALUES(?pserial, ?ppimage)"
        Dim com As New OdbcCommand(sInsertInto, connectme)

        com.Parameters.Add("pserial", OdbcType.VarChar).Value = pid
        com.Parameters.Add("?ppimage", OdbcType.VarBinary).Value = pimage

        connectme.Open()
        Dim result As Integer = com.ExecuteNonQuery()
        connectme.Close()

        If result > 0 Then
            lblimageup.Text = "Image saved."
        End If

    Else
        lblimageup.Text = "Please select an image file"

    End If
End Sub

当我执行此操作时,我收到如下的sytax错误:

ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.6.11]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'pserial, 'ÿØÿá/þExif\0\0MM\0*\0\0\0\0\0\0\0\0\0\0\0’\0\0\0\0   \0\' at line 1

我似乎无法找到任何语法错误。有任何想法吗?可能是图像文件中有什么东西会破坏语法吗?

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码,在@paramNameSQL中使用Patameters.Add

Dim sInsertInto As String = "INSERT INTO images(serial, image1) VALUES (@pserial, @ppimage)"
Dim com As New OdbcCommand(sInsertInto, connectme)

com.Parameters.Add("@pserial", OdbcType.VarChar).Value = pid
com.Parameters.Add("@ppimage", OdbcType.VarBinary).Value = pimage

答案 1 :(得分:0)

这看起来并不一致;我想你想做"?pserial"

com.Parameters.Add("pserial", OdbcType.VarChar).Value = pid
com.Parameters.Add("?ppimage", OdbcType.VarBinary).Value = pimage