' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.OleDb

Class MainClass

    Public Shared Sub Main(ByVal args As String())
        Dim con As OleDbConnection = New OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Database\MSAccess\Test.mdb;User Id=admin;Password=")
        con.Open
        Dim cre As OleDbCommand = New OleDbCommand ("CREATE TABLE imgtest (id INTEGER PRIMARY KEY,img OLEObject)", con)
        cre.ExecuteNonQuery
        Dim imgfile As Stream = New FileStream ("C:\blue.jpg", FileMode.Open)
        Dim siz As Integer = imgfile.Length
        Dim imgdata(siz-1) As Byte
        imgfile.Read(imgdata, 0, imgdata.Length)
        imgfile.Close
        Dim ins As OleDbCommand = New OleDbCommand ("INSERT INTO imgtest VALUES(@id,@img)", con)
        ins.Parameters.Add("@id", OleDbType.Integer)
        ins.Parameters.Add("@img", OleDbType.VarBinary)
        ins.Parameters("@id").Value = 1
        ins.Parameters("@img").Value = imgdata
        ins.ExecuteNonQuery
        Dim sel As OleDbCommand = New OleDbCommand ("SELECT img FROM imgtest WHERE id = @id", con)
        sel.Parameters.Add("@id", OleDbType.Integer)
        sel.Parameters("@id").Value = 1
        Dim imgdata2 As Byte() = CType(sel.ExecuteScalar, Byte())
        Dim drp As OleDbCommand = New OleDbCommand ("DROP TABLE imgtest", con)
        drp.ExecuteNonQuery
        Dim imgfile2 As Stream = New FileStream ("C:\blue2.jpg", FileMode.Create)
        imgfile2.Write(imgdata2, 0, imgdata2.Length)
        imgfile2.Close
        con.Close
    End Sub
End Class 
