Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / Visual Basic
Article

Download binary data from database using a Windows application

Rate me:
Please Sign up or sign in to vote.
2.40/5 (8 votes)
14 Mar 2005 44.3K   28   1
Function to download and open a file saved as IMAGE data in an SQL database.

Introduction

I was working on a web application for uploading contractor employee (35,000+) information over the internet, that included documents, images etc.. Internally, the organization would be using a VB.NET Windows application to view, verify and issue security clearance to these submissions. The ASP.NET part for Download/Upload to and from an SQL 2000 backend (table with a column of data type IMAGE), was pretty straightforward. Thanks to MS IE/browser functionality.

However with the Windows application, it was a totally different approach. It took me a bit of time before I was able to cook up something. I don't know if this is the best approach, however, I will list the source code for the benefit of anyone stuck in a similar situation.

Slap the below code into your application:

VB
Friend Function OpenDownloadDoc(ByVal fname As String, _
                                 ByVal bytBLOBData() As Byte) As Boolean
    Dim binwrt As BinaryWriter
    Try
        With binwrt
            Dim flstrm As FileStream = _
                         New FileStream(TempWorkDir & "\" & fname, _
                         FileMode.OpenOrCreate, FileAccess.Write)
            binwrt = New BinaryWriter(flstrm)
            .Write(bytBLOBData)
            .Flush()
            .Close()
            flstrm.Close()
            System.Diagnostics.Process.Start(TempWorkDir & "\" & fname)
        End With
        Return True
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    End Try
End Function

The function parameters are a file name (with extension) and the binary data in a Byte().

VB
System.Diagnostics.Process.Start(TempWorkDir & "\" & fname)

The above line forces the file to be opened using the appropriate application registered for use with the file extension. TempWorkDir is a temporary folder (cache) assigned to the application.

VB
Friend Sub FlushTempDir()
    Dim fd As Directory
    Dim fl As String
    Dim xfl As File
    For Each fl In fd.GetFiles(TempWorkDir)
    xfl.Delete(fl)
    Next
End Sub

Call the above function when the application quits, to clean the cache.

Simple? Hope it helps someone out there...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to get TempWorkDir for my application Pin
skajaykumar7-May-07 1:24
skajaykumar7-May-07 1:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.