Solved

How do I open a PDF Blob field in SQL Server and open the file using VB.Net?

Posted on 2014-01-13
7
956 Views
Last Modified: 2014-01-14
I have a SQL Server table that contains a BLOB column (PDF files are stored in this column). I need to retrieve it using VB.Net and open it as a PDF File. Any ideas?

Thanks!
0
Comment
Question by:BlakeMcKenna
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:ktaczala
ID: 39778208
Open it as a filesteam, write it to disk as "c:\temp\tmpfile.pdf" then run process.start("c:\temp\tmpfile.pdf") that will open the file with the default PDF app.
0
 
LVL 12

Expert Comment

by:ktaczala
ID: 39778239
A little more detail.

load the blob into a datatable:
get the image.length into a variable
get the # of bytes of the image.
read it into a memorystream
write it to a filestream
 then open with process.start

for example (this is a guide)
        If ImageDataGridView.Rows.Count = 0 Then 'no row exists
            Exit Sub
        End If

        Dim curimageID = row.ImageID
        currentImageID = curimageID
        Dim _dtImage As New DSDocuments.ImageDataTable
        Dim _taImage As New DSDocumentsTableAdapters.ImageTableAdapter
        Dim _drImage As DSDocuments.ImageRow
        Dim _intLength As Integer
        Dim _imageBytes() As Byte
        Try
            _taImage.FillImagesByImageID(_dtImage, curimageID)
            If _dtImage.Count > 0 Then
                _drImage = _dtImage.Rows(0)
                _intLength = _drImage.Image.Length
                _imageBytes = _drImage.Image     ' database value into bytes
                Dim _originalImageMemoryStream As New System.IO.MemoryStream(_imageBytes, 0, _imageBytes.Length) ' bytes --> stream
                If Not Directory.Exists(tempfilepath) Then
                    Directory.CreateDirectory(tempfilepath)
                End If
                If File.Exists(tempfilepath & "\temp" & tempincrement.ToString & fileextension) Then
                    Try
                        Dim _fs1 As FileStream = File.OpenWrite(tempfilepath & "\temp" & tempincrement.ToString & fileextension)
                        _fs1.Close()
                        File.Delete(tempfilepath & "\temp" & tempincrement.ToString & fileextension)
                    Catch ex As Exception
                        tempincrement += 1
                    End Try
                End If
                viewercurrentfilename = tempfilepath & "\temp" & tempincrement.ToString & fileextension
                Dim _fs As FileStream = File.OpenWrite(viewercurrentfilename)
                _originalImageMemoryStream.WriteTo(_fs)
                _fs.Close()
                            System.Diagnostics.Process.Start(viewercurrentfilename)
0
 

Author Comment

by:BlakeMcKenna
ID: 39779290
Ok, I tried what you suggested in your 1st post. However, I get an error saying:

"A required privilege is not held by the client".

What is that? I'm writing the file to my C: drive and I'm the administrator of my own machine. Never seen this one before.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 12

Accepted Solution

by:
ktaczala earned 500 total points
ID: 39779306
save it to an existing folder not in the root
0
 

Author Comment

by:BlakeMcKenna
ID: 39779320
That worked but why can't I save it to the root drive?
0
 
LVL 12

Expert Comment

by:ktaczala
ID: 39779343
Windows(microsoft) just don't want you to save there by default.
0
 

Author Comment

by:BlakeMcKenna
ID: 39779476
That works for me!

Thanks again!
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question