2014-01-29 56 views
0

我正在开发vb.net和MS Access中的应用程序。起初,在几个小时之后,我使用一些资源将代码放在一起,从数据库中打开一个pdf文件。代码起作用。但是当我从应用程序午餐时,它会在根文件夹中创建一个pdf文件。所以现在我把它作为二进制数据库(OLEObjects)和根文件夹。我只需要它在一个地方。下面是代码:从MS Access打开PDF,使用VB.NET中的窗体形式

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 
+0

什么是你要求?你正在告诉应用程序打开一个文件,该应用程序的写作方式很可能是它可以打开PDF文件,而不是数据库中的任何内容......那么,你想要做的究竟是什么?你不能将文件从数据库中获取到系统的临时文件夹中,以便自动清除它,或者在不再使用时将其清理干净。 –

+0

如果我不清楚,我很抱歉。我想停止在我的数据库中存储pdf,而不是将所有pdf文件放在一个文件夹中,而在数据库中只是有一个路径到该文件夹​​。我得到它的工作。 – Victor

回答

0
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