0

我正在使用 vb.net 并访问 2007。我想将 pdf 文件路径保存在数据库中,并希望在单击按钮或图像时检索路径并读取 pdf 文件。

我有一个表单,我可以在其中浏览并提供 pdf 文件的路径,当我单击保存按钮时,应该读取文本框中的路径,并且应该将 pdf 文件复制到项目文件夹中的某个位置,并且路径应该保存在数据库。当我想检索数据时,我应该获取 pdf 文件以及其他详细信息,当我单击按钮或图像时,我应该能够仅以 pdf 格式读取 pdf 文件,而不是在文本框中。这是代码我试图将文件从一个位置复制到另一个位置并将路径保存在数据库中。

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    ' Specify the directories you want to manipulate.
    Dim path As String = txtSLAPath.Text
    Dim path2 As String = "E:\" + "SLA1.pdf"
    Try
        Dim fs As FileStream = File.Create(path, 1024)
        fs.Close()

        'Copy the file.
        File.Copy(path, path2)

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    Try
        Dim insQry As String
        insQry = "insert into SLAdb (PO,SLA)values(@PO,@SLA)"

        cnnOLEDB.Open()

        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(insQry, cnnOLEDB)

        cmd.Parameters.Add(New OleDb.OleDbParameter("@PO", txtPO.Text))
        cmd.Parameters.Add(New OleDb.OleDbParameter("@SLA", path2))

        cmd.ExecuteNonQuery()
        MsgBox("Data Saved Successfully")

        cnnOLEDB.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

文件被复制并保存路径,但请建议我阅读路径和文件。

4

3 回答 3

0

两个文件都变成零字节

当然,您可以在源文件的相同路径上创建一个新文件,而无需写入任何内容。
然后,您关闭 FileStream 并将路径(现在归零的文件)传递给 File.Copy

' Specify the directories you want to manipulate.
Dim path As String = txtSLAPath.Text
Dim path2 As String = "E:\" + "SLA1.pdf"
Try

    ' The following line destroy the source file path
    ' the subsequent File.Copy, copies nothing in the destination path

    'Dim fs As FileStream = File.Create(path, 1024)
    'fs.Close()

    'Copy the file.
    File.Copy(path, path2)

Catch ex As Exception
    MsgBox(ex.Message)
End Try
于 2013-03-30T10:19:13.423 回答
0

您再次creating a file使用pathname源文件相同的 and 。我认为这可能是问题所在。试试下面的代码,

 Dim path As String = txtSLAPath.Text
 Dim path2 As String = "E:\" + "SLA1.pdf"
    Try
        'Remove this, but i'm not sure why did you use this here.
        'Dim fs As FileStream = File.Create(path, 1024)
        'fs.Close()

        'Copy the file.
        File.Copy(path, path2)

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
于 2013-03-30T10:19:34.923 回答
0

在新窗口中打开文件的代码。

'获取保存在db中的文件路径的代码..

                Dim drPdf As OleDb.OleDbDataReader
                Dim cmd1 As OleDb.OleDbCommand

                Dim pdf As String = ("SELECT * FROM SLAdb where PO='" & txtPo.Text & "'")
                cnnOLEDB.Open()
                cmd1 = New OleDb.OleDbCommand(pdf, cnnOLEDB)
                drPdf = cmd1.ExecuteReader

            If drPdf.Read = True Then
                fpath = drPdf("SLA")
            End If

'从路径获取文件名的代码

                fname = Path.GetFileName(fpath)
                lblSLA.Text = fname
            cnnOLEDB.Close()

'打开路径保存在db中的pdf文件的代码

    System.Diagnostics.Process.Start(fpath)
于 2013-04-09T05:24:49.983 回答