Database Structure:
--------------------------------------
Test. mdb [put it in the bin directory]
Table Test (ID automatic number, img ole)
----------------------------------------------
Code:
----------------------------------------------------
Use an openfiledialog with two pictureboxes
----------------------------------------------------------------------
Imports system. Io
Public class form1
Inherits system. Windows. Forms. Form
Dim CNN as data. oledb. oledbconnection
Private sub button4_click (byval sender as system. Object, byval e as system. eventargs) handles button4.click
Me. Dispose (true)
End sub
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
If OpenFileDialog1.ShowDialog = DialogResult. OK Then
PictureBox1.Image = Image. FromFile (OpenFileDialog1.FileName)
End If
End Sub
Private Sub DBInit ()
Try
Cnn = New Data. OleDb. OleDbConnection ("Provider = Microsoft. JET. OLEDB.4.0; Data Source =" & Application. StartupPath & "/test. mdb ")
Cnn. Open ()
Catch exp As OleDb. OleDbException
MsgBox (exp. Message)
End
End Try
End Sub
Private Sub DBRelease ()
Cnn. Close ()
Cnn = Nothing
End Sub
Private Sub Button2_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button2.Click
If PictureBox1.Image Is Nothing Then
MsgBox ("select image first", MsgBoxStyle. Exclamation)
Exit Sub
End if
Dim FS as filestream = new filestream (openfiledialog1.filename, filemode. Open, fileaccess. Read)
Dim Bt (FS. Length) as byte
FS. Read (BT, 0, FS. length)
FS. Close ()
FS = nothing
Dim olecmd as oledb. oledbcommand = new oledb. oledbcommand
Dbinit ()
Olecmd. Connection = CNN
Olecmd. commandtype = commandtype. Text
Olecmd. commandtext = "insert into test (IMG) values (@ IMG )"
Olecmd. Parameters. Add ("@ IMG", oledb. oledbtype. Binary). value = BT
Olecmd. executenonquery ()
Olecmd = nothing
Dbrelease ()
Msgbox ("image inserted successfully ")
End sub
Private sub button3_click (byval sender as system. Object, byval e as system. eventargs) handles button3.click
Dim oleCmd As OleDb. OleDbCommand = New OleDb. OleDbCommand ("SELECT img FROM test WHERE id = 1 ")
OleCmd. CommandType = CommandType. Text
DBInit ()
OleCmd. Connection = cnn
Dim dr As OleDb. OleDbDataReader = oleCmd. ExecuteReader (CommandBehavior. SingleRow)
If dr. Read Then
If Not IsDBNull (dr. Item (0) Then
Dim bt () As Byte = dr. Item (0)
Dim MS As MemoryStream = New MemoryStream (bt)
PictureBox2.Image = Image. FromStream (MS)
Else
MsgBox ("no image ")
End If
Else
MsgBox ("no data ")
End If
Dr. Close ()
Dr = Nothing
OleCmd = Nothing
DBRelease ()
End Sub
End Class