【问题标题】:Open PDF from MS Access, using windows forms in VB.NET在 VB.NET 中使用 windows 窗体从 MS Access 打开 PDF
【发布时间】:2014-01-30 23:36:57
【问题描述】:

我正在 vb.net 和 MS Access 中开发应用程序。起初,几个小时后,我使用一些资源将代码放在一起以从数据库中打开一个 pdf 文件。该代码有效。但是当我从应用程序中获取 pdf 文件时,它会在根文件夹中创建一个 pdf 文件。所以现在我将它作为二进制(OLEObjects)放在数据库和根文件夹中。我只需要它在一个地方。代码如下:

    Dim strSqll As String
    Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BCInventory.accdb;Persist Security Info=False;")
    conn.Open()


    strSqll = "Select filename from pdf WHERE PR = '" & pr & "' AND Year = '" & year & "'"

    Dim sqlCmdd As New OleDbCommand(strSqll, conn)

    Dim r As OleDb.OleDbDataReader = sqlCmdd.ExecuteReader()

    If Not r.HasRows Then Exit Sub

    Do While r.Read()
        fname = r.GetString(0)
    Loop
    conn.Close()

       Try
            Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BCInventory.accdb;Persist Security Info=False;")
            con.Open()
            Dim strSql As String = "Select File from pdf WHERE PR = '" & pr & "' AND Year = '" & year & "' AND fileType='" & ftype & "'"

            Dim sqlCmd As New OleDbCommand(strSql, con)

            Dim fileData As Byte() = DirectCast(sqlCmd.ExecuteScalar(), Byte())

            Dim sTempFileName As String = Application.StartupPath & "\Purchase Orders scans" & "\" & fname

            If Not fileData Is Nothing Then
                Using fs As New FileStream(fname, FileMode.OpenOrCreate, FileAccess.Write)
                    fs.Write(fileData, 0, fileData.Length)
                    fs.Flush()
                    fs.Close()
                End Using
                ShellEx(Me.Handle, "Open", fname, "", "", 10)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

【问题讨论】:

  • 你要什么?您正在告诉应用程序打开一个文件,该应用程序很可能是以这样一种方式编写的,即它可以打开 PDF 文件,但不能从数据库中打开……那么您究竟想要做什么?你不能把数据库中的文件放到系统的临时文件夹中,让它自动清理,或者当它不再使用时你可以清理它吗?
  • 如果我不清楚,我很抱歉。我想停止将 pdf 存储在我的 db 中,而是希望将所有 pdf 文件放在一个文件夹中,并且在 db 中只有一个指向该文件夹的路径。我让它工作了。

标签: vb.net pdf


【解决方案1】:
Dim strSqll As String
Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BCInventory.accdb;Persist Security Info=False;")
conn.Open()


strSqll = "Select filename from pdf WHERE PR = '" & pr & "' AND Year = '" & year & "'"

Dim sqlCmdd As New OleDbCommand(strSqll, conn)

Dim r As OleDb.OleDbDataReader = sqlCmdd.ExecuteReader()

If Not r.HasRows Then Exit Sub

Do While r.Read()
    fname = r.GetString(0)
Loop
conn.Close()

    Try
         If Len(Trim(fname)) = 0 Then
                MessageBox.Show("There is no file attached", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtpr.Focus()
                Exit Sub
            End If
         ShellEx(Me.Handle, "Open", fname, "", "", 10)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多