在c#WinForms中保存并查看SQL Server数据库中的pdf文件 [英] Save and view pdf file from SQL server database in c# WinForms

查看:91
本文介绍了在c#WinForms中保存并查看SQL Server数据库中的pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在开发一个Windows应用程序,我需要浏览pdf文件并将它们直接存储到数据库中,而不是存储文件路径。任何人都可以告诉我如何将文件直接存储到数据库中,然后将其检索回来并在pdf查看器中显示或在需要时以某种类似的控件显示?



提前感谢......





问候,

Aradhana

Hi all,
I am working on a windows application where i need to browse pdf files and store them directly into the database instead of storing file path. can anyone please tell me how can i store the file directly into the database and then also retrieve it back and show it in pdf viewer or in some similar control when needed ??

thanks in advance......


Regards,
Aradhana

推荐答案

Imports System.Data.SqlClient

'IF OBJECT_ID('<strong class="highlight">PDF</strong>', 'U') IS NOT NULL DROP TABLE <strong class="highlight">PDF</strong>
'CREATE TABLE <strong class="highlight">PDF</strong>
'(
'  RecordId int identity(1000, 1) PRIMARY KEY,
'  <strong class="highlight">PDF</strong> varbinary(max)
')

Public Class frmPDF

  Private Sub ButtonUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpload.Click
    Using conn As New SqlConnection("<strong class="highlight">Data</strong> Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;")
      conn.Open()
      Using cmd As New SqlCommand("Insert Into <strong class="highlight">PDF</strong> (<strong class="highlight">PDF</strong>) Values (@PDF)", conn)
        cmd.Parameters.Add(New SqlParameter("@PDF", SqlDbType.VarBinary)).Value = System.IO.File.ReadAllBytes("C:\file.pdf")
        cmd.ExecuteNonQuery()
      End Using
      conn.Close()
    End Using
  End Sub

  Private Sub ButtonDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDownload.Click
    Dim sFilePath As String
    Dim buffer As Byte()
    Using conn As New SqlConnection("<strong class="highlight">Data</strong> Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;")
      conn.Open()
      Using cmd As New SqlCommand("Select Top 1 <strong class="highlight">PDF</strong> <strong class="highlight">From</strong> <strong class="highlight">PDF</strong>", conn)
        buffer = cmd.ExecuteScalar()
      End Using
      conn.Close()
    End Using
    sFilePath = System.IO.Path.GetTempFileName()
    System.IO.File.Move(sFilePath, System.IO.Path.ChangeExtension(sFilePath, ".pdf"))
    sFilePath = System.IO.Path.ChangeExtension(sFilePath, ".pdf")
    System.IO.File.WriteAllBytes(sFilePath, buffer)
    Dim act As Action(Of String) = New Action(Of String)(AddressOf OpenPDFFile)
    act.BeginInvoke(sFilePath, Nothing, Nothing)
  End Sub

  Private Shared Sub OpenPDFFile(ByVal sFilePath)
    Using p As New System.Diagnostics.Process
      p.StartInfo = New System.Diagnostics.ProcessStartInfo(sFilePath)
      p.Start()
      p.WaitForExit()
      Try
        System.IO.File.Delete(sFilePath)
      Catch
      End Try
    End Using
  End Sub
End Class


检查以下文章&代码和用法的线程:



http://www.dotnetspider.com/projects/512-Store-Retrieve-pdf-txt-doc-Images-Sql-server-database.aspx [ ^ ]



使用winforms和C#保存,查看,删除文件 [ ^ ]
Check the following article & thread for code and usage:

http://www.dotnetspider.com/projects/512-Store-Retrieve-pdf-txt-doc-Images-Sql-server-database.aspx[^]

save,view, remove file using winforms and C#[^]


请参阅此链接



http://www.c-sharpcorner.com/blogs/6287/save-pdf-file-in-sql-server-database-using-c-sharp.aspx [ ^ ]


这篇关于在c#WinForms中保存并查看SQL Server数据库中的pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆