Save image in database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnemisylpaler
    New Member
    • Jul 2007
    • 3

    Save image in database

    can you teach me how to convert to binary the picture in a picture box in order to save and retrieve to database acces ? help me plsss
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by johnemisylpaler
    can you teach me how to convert to binary the picture in a picture box in order to save and retrieve to database acces ?
    Just have a look in the index of VB articles, at the top of the forum. There are a couple of entries there about storing and retrieving images in BLOBs, which are large binary database fields (Binary Large OBject, I believe).

    Comment

    • kentgorrell
      New Member
      • Jul 2007
      • 11

      #3
      To store an image from a file use this -
      [code=VB]Dim cn As ADODB.Connectio n, rs As ADODB.Recordset , SQL As String
      Dim ps As New ADODB.Stream
      ps.Type = adTypeBinary

      Set cn = New ADODB.Connectio n
      Set rs = New ADODB.Recordset
      cn.CursorLocati on = adUseServer
      cn.Open "dsn=SQLDatabas e"

      SQL = "SELECT * FROM Student WHERE Student_ID = " & 1002
      rs.Open SQL, cn, adOpenStatic, adLockOptimisti c
      'rs.addnew
      ps.Open
      ps.LoadFromFile "C:\My Documents\My Pictures\Studen t1002.JPG"
      rs!Student_Phot o = ps.Read
      rs.Update[/CODE]
      Note - this uses the streaming object so you need a reference to ADO (MS Data ActiveX Data Objects) 2.6 or greater
      To read it back out to a file that you can then reference as the source for a picture control -
      [CODE=vb]Dim cn As ADODB.Connectio n
      Dim rs As ADODB.Recordset
      Dim ps As New ADODB.Stream
      Dim SQL As String

      ps.Type = adTypeBinary
      Set cn = New ADODB.Connectio n
      Set rs = New ADODB.Recordset

      cn.CursorLocati on = adUseServer
      cn.Open "dsn=SQL Database"

      SQL = "SELECT * FROM Student WHERE Student_ID = " & Me.txtStudent_I D
      rs.Open SQL, cn, adOpenStatic, adLockReadOnly
      ps.Open

      ps.Write rs!Student_Phot o
      ps.SaveToFile "C:\Student_Pho to.JPG", adSaveCreateOve rWrite[/code]
      Last edited by Killer42; Jul 30 '07, 10:16 PM. Reason: Added [CODE=vb] tags

      Comment

      Working...