Database structure: -------------------------------------- Test.mdb [put in bin directory ] Table Test (ID Auto Number, IMG OLE) --------------------------------------- ------- Code: ----------------------------------------- ----------- Using an OpenFileDialog, two Picturebox ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------- Imports System.iopublic Class Form1 Inherits System.Windows.Forms .Form.oledbconnection private sub button4_click (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.OLDBEXCEPTION MSGBOX (Exp.Message) End end 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 a picture", 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 Insert Success") End Sub