Home Forums SQL Server 2008 T-SQL (SS2K8) Help required to extra image from varbinary column and create image on file system RE: Help required to extra image from varbinary column and create image on file system

  • here's some .NET code i posted from a different post on the same issue;

    you can use it as a model if you have the file name and binary data together, ie Whatever.pdf, or myprogram.exe, or myimage.jpg...

    Private Sub btnBlobsToDisk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBlobsToDisk.Click

    '--Imports System.IO

    '--Imports System.Data.SqlClient

    '--this works whether the datatype for is IMAGE or VARBINARY(max)

    Dim sqlcmd As String = "SELECT TOP 3 FileName,ImageData From BatchOfBlobs;"

    Dim mySqlConnectionFormat As String = "data source={0};initial catalog={1};user id={2};password={3};Trusted_Connection=False;Connect Timeout=600;Workstation ID=GhostInTheMachine;Application Name=HaxxorPadPlusPlus;"

    Dim MyConn As New SqlConnection

    MyConn.ConnectionString = String.Format(mySqlConnectionFormat, "DEV223", "SandBox", "Noobie", "NotARealPassword")

    MyConn.Open()

    'now lets get a commadn object

    Dim mySqlCommand As New SqlCommand

    mySqlCommand.Connection = MyConn

    mySqlCommand.CommandTimeout = 600

    mySqlCommand.CommandType = CommandType.Text

    mySqlCommand.CommandText = sqlcmd

    Dim myDataReader As SqlDataReader

    myDataReader = mySqlCommand.ExecuteReader

    While myDataReader.Read

    Try

    Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    Dim imageInBytes As Byte() = myDataReader("ImageData")

    Dim memoryStream As System.IO.Stream = New System.IO.MemoryStream(imageInBytes, True)

    Dim image As New System.IO.BinaryWriter(File.Open(path & "\" & myDataReader("FileName"), FileMode.Create))

    '%APPDATA% variable like "C:\Users\Lowell\AppData\Roaming"

    image.Write(imageInBytes)

    image.Flush()

    image.Close()

    Catch ex As Exception

    Console.WriteLine(ex.StackTrace)

    End Try

    End While

    End Sub

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!