使用visual basic在mysql中保存blob

时间:2014-10-01 14:08:16

标签: mysql sql winforms visual-studio vba

我有一个pdf文件,我将其转换为字节,但它说:

There was an error: Invalid data type

我不知道如果我做错了,可以用这种方式保存pdf吗? 我的数据库是这个

CREATE TABLE emision_modificacion_eliminacion(
id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
file_name VARCHAR(64) NOT NULL,
file_size MEDIUMINT UNSIGNED NOT NULL,
file MEDIUMBLOB NOT NULL);

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)               Handles   Button2.Click
    Dim fs As FileStream
    Dim FileSize As UInt32
    Dim rawData() As Byte
    Dim cmd As New MySQLCommand
    Try
        fs = New FileStream("C:\Objetivos.pdf", FileMode.Open, FileAccess.Read)            
        FileSize = fs.Length
        rawData = New Byte(FileSize) {}
        fs.Read(rawData, 0, FileSize)
        fs.Close()
        cmd = conxv.crear_consulta(cnn, "INSERT INTO emision_modificacion_eliminacion VALUES(NULL,'Objetivos.pdf', ?FileSize,?File)")
        cmd.Parameters.Add("?FileSize", FileSize)
        cmd.Parameters.Add("?File", rawData)
        cmd.ExecuteNonQuery()
        MessageBox.Show("File Inserted into database successfully!", _
        "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        cnn.Close()
    Catch ex As Exception
        MessageBox.Show("There was an error: " & ex.Message, "Error", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        cnn.Close()
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

你确定这是导致你麻烦的BLOB吗?

您不需要为AUTO_INCREMENT类型传递任何内容,因此我会像这样重写SQL:

"INSERT INTO emision_modificacion_eliminacion VALUES('Objetivos.pdf', ?FileSize, ?File)"

可能是导致错误的null