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
Save image in database
Collapse
X
-
Tags: None
-
Originally posted by johnemisylpalercan you teach me how to convert to binary the picture in a picture box in order to save and retrieve to database acces ? -
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]Comment
Comment