dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to Store IMAGES in Sql Server 2005

  1. #1
    Join Date
    Oct 2006
    Location
    Hyderabad
    Posts
    6

    How to Store IMAGES in Sql Server 2005

    HI to all,
    Please telme, How to store Images in Sql Server 2005?
    [From Sql Server Management Studio]
    Thanks in advance.

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Don't know SQL Server but in Oracle I used a Data Type call a blob, which
    stands for binary large objects. To actually load the image file into the field
    I used the ADO Streams object. At the time I was using Access as the
    front end to the Oracle database but the code should work in VB as well
    as it did in VBA. Here are Read and Write functions that take a recordset
    and the file name as parameters. The current record is expected to contain
    a field named BLOB_BINARYFILE in which the file is stored or retreieved.
    You can of course change that name to anything you like. Here are the
    functions:
    Code:
    'Store Image File into a field of a recordset
    Public Function ReadFromFile(rs As ADODB.Recordset, str_full_path As String)
      On Error GoTo Er
      Dim fds As ADODB.Stream
    
      ReadFromFile = False
      
      Set fds = New ADODB.Stream
      'Make it a binary type
      fds.Type = adTypeBinary
      'Open the stream
      fds.Open
      '
      '*** Read the binary file into the stream buffer ***
      '
      fds.LoadFromFile str_full_path
      ' save binary data into Field of current record
      rs!BLOB_BINARYFILE = fds.Read
      rs.Update
      
      ReadFromFile = True
        
    Done:
      fds.Close
      Set fds = Nothing
      Exit Function
    
    Er:
      Select Case Err.Number
        Case 55
          Close
          Resume
        Case 3002
          MsgBox "Could not read file (" & str_full_path & ") , check the path or the file may be in use."
        Case Else:
          'Unexpected, fail with message box
          MsgBox "Error # " & Err.Number & "--" & Error, vbCritical, "ReadFromFile()"
      End Select        
      Resume Done
    End Function
    
    'Retreive Image From Field and save to file 
    Public Function WriteToFile(rs As ADODB.Recordset, str_full_path As String) As Boolean
      On Error GoTo Er
      
      Dim fds As ADODB.Stream
        
      WriteToFile = False
      
      Set fds = New ADODB.Stream
      fds.Type = adTypeBinary
      fds.Open
      'get data from field in current record
      fds.Write rs!BLOB_BINARYFILE
      fds.SaveToFile str_full_path, adSaveCreateOverWrite
      
      WriteToFile = True
        
    Done:
      fds.Close
      Set fds = Nothing
      Exit Function
    
    Er:
      Select Case Err.Number
        Case 3004: ' file is in use!
          MsgBox "File is either already in use or it's file permissions are incorrect.  Check the path or the file may be in use.", vbExclamation, "WriteToFile()"
          
        Case Else:
          'Unexpected, fail with message box
          MsgBox "Error # " & Err.Number & "--" & Error, vbCritical, "WriteToFile()"
      End Select
            
      Resume Done
    
    End Function

Similar Threads

  1. Access & SQL Server
    By David Jones in forum Database
    Replies: 0
    Last Post: 08-31-2001, 12:22 PM
  2. Changing Server Name
    By Levi Nkata in forum Database
    Replies: 3
    Last Post: 04-11-2001, 09:31 AM
  3. SQL Server Agent wont auto start
    By David Hay in forum Database
    Replies: 2
    Last Post: 04-02-2001, 07:11 PM
  4. myLittleAdmin SQL Server version
    By myLittleTools.net in forum web.announcements
    Replies: 0
    Last Post: 03-10-2001, 01:35 PM
  5. How to store Images in SQL Server ???
    By SQLNewBie in forum Database
    Replies: 1
    Last Post: 05-30-2000, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center