How to retrieve a .jpg file from a SQL database and save it to a file locally

Posted on 2005-03-03
Medium Priority
Last Modified: 2010-04-23
I need to retrieve a .jpg file from a SQL database and save it to a file locally  How do I do it using VB.Net?
Question by:RMAHelpDesk
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 19

Accepted Solution

arif_eqbal earned 2000 total points
ID: 13456222
try this...

        Dim cn As New SQLConnection(ConStr) 'Your Connection
        Dim Cmd As New SQLCommand("SELECT ID,MyImage FROM MyTable", cn) 'Change Table and field names

        Dim DA As New SQLDataAdapter(Cmd)
        Dim DT As New DataTable("MyTable")

        Catch ex As Exception
        End Try

        Dim i As Integer = DT.Rows.Count
        If i > 0 Then
            Dim BTImage() As Byte = DT.Rows(0)("MyImage")
            Dim MSImage As New MemoryStream(BTImage)
           Image.FromStream(MSImage).Save("C:\YOUR PATH", Drawing.Imaging.ImageFormat.Jpeg)
'Give Path and Format
        End If

I had this code running for Oledb I just changed the OleDB to SQL, hopefully it should work....

Author Comment

ID: 13460046
It worked!
I did have to add       Imports System.IO

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 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