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
939 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now