对于某些文件,VB.NET MySQL blob保存失败

时间:2013-05-10 11:19:04

标签: mysql vb.net blob

有时将使用VB.NET的BLOB保存到MySQL数据库失败。 我正在保存PDF。

当我在计算机上打开PDF时,效果很好。 然后我将它保存为db,并且通常它可以找到。 我再次从db中检索pdf,将其保存到文件中,然后我可以打开它。

但有时Acrobat Reader不想打开新保存的PDF,说该文件已损坏。

我不确定我的代码中有什么缺陷。 如果有人可以提供帮助,那将是非常好的。 谢谢。

Private Function pUpdateInvoice(ByVal uGUID As String, ByVal uPath As String) As Boolean

    Dim rawData() As Byte
    Dim fs As FileStream

    Try
        fs = New FileStream(uPath, FileMode.Open, FileAccess.Read)

        Dim iFileSize As UInt32
        iFileSize = fs.Length

        rawData = New Byte(iFileSize) {}
        fs.Read(rawData, 0, iFileSize)
        fs.Close()

        If Not g_CnWebDB.Ping Then
            InitMySQL()
        End If

        Dim sFileName$
        sFileName = CleanFile(uPath, False)

        Dim sSQL
        sSQL = "UPDATE expenses SET " & _
            "expense_invoicename=@expense_invoicename," & _
            "expense_invoicefilesize=@expense_invoicefilesize," & _
            "expense_invoiceblob=@expense_invoiceblob," & _
            "expense_invoicetype=@expense_invoicetype, " & _
            "expense_invoiceexistsinguid=@expense_invoiceexistsinguid " & _
            "WHERE " & _
            "expense_guid=@expense_guid"

        Dim cmd As New MySqlCommand
        cmd.Connection = g_CnWebDB
        cmd.CommandText = sSQL
        cmd.Parameters.AddWithValue("expense_invoicename", sFileName)
        cmd.Parameters.AddWithValue("expense_invoicefilesize", iFileSize)
        cmd.Parameters.AddWithValue("expense_invoiceblob", rawData)
        cmd.Parameters.AddWithValue("expense_invoicetype", eInvoiceType.eIT_Digital)
        cmd.Parameters.AddWithValue("expense_invoiceexistsinguid", "")
        cmd.Parameters.AddWithValue("expense_guid", uGUID)

        cmd.ExecuteNonQuery()

        Return True

    Catch ex As Exception
        MessageBox.Show("There was an error: " & ex.Message, "Error", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    Return False

End Function

 Private Sub pShowPDF(ByVal uGUID As String)

    If Not g_CnWebDB.Ping Then
        InitMySQL()
    End If

    Dim cmdSel As New MySqlCommand("SELECT * FROM expenses WHERE expense_guid=" & Apo(uGUID), g_CnWebDB)
    Dim r As MySqlDataReader
    r = cmdSel.ExecuteReader

    If Not r.HasRows Then
        Stop
    End If

    r.Read()

    Dim FileSize As UInt32
    FileSize = r.GetUInt32(r.GetOrdinal("expense_invoicefilesize"))
    Dim rawData() As Byte
    rawData = New Byte(FileSize) {}

    r.GetBytes(r.GetOrdinal("expense_invoiceblob"), 0, rawData, 0, FileSize)
    r.Close()

    Dim sPath$
    sPath = "m:\temp.pdf"
    modIO.DeleteFile(sPath)

    Dim fs As FileStream
    fs = New FileStream(sPath, FileMode.OpenOrCreate, FileAccess.Write)
    fs.Write(rawData, 0, FileSize)
    fs.Close()

    Dim id As Integer
    id = System.Diagnostics.Process.Start(sPath).Id

End Sub

1 个答案:

答案 0 :(得分:0)

我选择“blob”作为db中的列类型。 相反,我应该选择“longblob”。