Access database structure:
--------------------------------------
Test.mdb [put in bin directory]
Table Test (ID AutoNumber, IMG OLE)
----------------------------------------------
Code:
----------------------------------------------------
Using a OpenFileDialog, two PictureBox
----------------------------------------------------------------------
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 button1_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 ("Please select pictures 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 ("Picture Insert succeeded")
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 Pictures")
End If
Else
MsgBox ("No Data")
End If
Dr. Close ()
Dr = Nothing
Olecmd = Nothing
Dbrelease ()
End Sub
End Class
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.