0

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

当我在我的电脑上打开 PDF 时,效果很好。然后我将它保存为 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
4

1 回答 1

0

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

于 2013-05-10T11:58:14.577 回答