Click here to Skip to main content
13,202,960 members (57,906 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 14 Mar 2005

Download binary data from database using a Windows application

, 14 Mar 2005
Rate this:
Please Sign up or sign in to vote.
Function to download and open a file saved as IMAGE data in an SQL database.


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:

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

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

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.

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

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

Simple? Hope it helps someone out there...


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


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHow to get TempWorkDir for my application Pin
skajaykumar7-May-07 1:24
memberskajaykumar7-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 14 Mar 2005
Article Copyright 2005 by SUNILT
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid