Results 1 to 3 of 3

Thread: Retrieve Binary File from SQL Server

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    1

    Retrieve Binary File from SQL Server

    Hi,

    I want to retrieve a binary file from SQL Server using ADO.NET. How can I accomplish this?

    Thanks in advance.
    Regards,
    N Y Prakash

  2. #2
    Hyperactive Member scuzymoto's Avatar
    Join Date
    Aug 1999
    Location
    Washington State
    Posts
    316
    These are the steps I used.

    1. Create a dataset control from your data tab. Put a table in it with a single image field (really a binary field).

    2. Create a connection to your SQL Database with a dataadapter control.

    3. Fill the dataset field with the image field from your SQL Database.

    4. Use a binary writer to put the contents to a file.

    My example below retrieves a JPG from my database and saves it to a file called temp.jpg. It assumes you have a dataset called dsTables with a table called Image and a dataadapter called sdaImage with a table called images and two fields, one called image and one called image_number.

    Code:
            dsTables.Tables("Image").Clear()
    
            Dim fsImage As New System.IO.FileStream("test.jpg", System.IO.FileMode.Create)
            Dim bwImage As New System.IO.BinaryWriter(fsImage)
    
            sdaImage.SelectCommand.CommandText = "select image from images where image_number = '12345' "
    
            Dim nRecords as Int16  ' holds number of records returned by fill
            nRecords sdaImage.Fill(dsTables, "Image")  'fill dataset with binary blob
    
            If nRecords > 0 Then
                bwImage.Write(dsTables.Tables("Image").Rows(0).Item(0))  'retrieve binary blob from dataset
            End If
    
            bwImage.Flush()  'write binary blob to file specified above
            bwImage.Close()
    
            fsImage = Nothing
            bwImage = Nothing
    Last edited by scuzymoto; Oct 29th, 2003 at 06:16 PM.
    SCUZ

  3. #3
    Banned
    Join Date
    Jul 2003
    Location
    New delhi
    Posts
    143
    Hi prakash

    you use stream object here
    in .net we can use the stream object for binary file and alos picture object

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width