' 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.SqlClient

Class MainClass

    Public Shared Sub Main(ByVal args As String())
        Dim con As SqlConnection = New SqlConnection ("server=ARNEPC2\ARNEPC2RUN;Integrated Security=SSPI;database=TestMSDE")
        con.Open
        Dim cre As SqlCommand = New SqlCommand ("CREATE TABLE imgtest (id INTEGER PRIMARY KEY,img IMAGE)", 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 SqlCommand = New SqlCommand ("INSERT INTO imgtest VALUES(@id,@img)", con)
        ins.Parameters.Add("@id", SqlDbType.Int)
        ins.Parameters.Add("@img", SqlDbType.Image)
        ins.Parameters("@id").Value = 1
        ins.Parameters("@img").Value = imgdata
        ins.ExecuteNonQuery
        Dim sel As SqlCommand = New SqlCommand ("SELECT img FROM imgtest WHERE id = @id", con)
        sel.Parameters.Add("@id", SqlDbType.Int)
        sel.Parameters("@id").Value = 1
        Dim imgdata2 As Byte() = CType(sel.ExecuteScalar, Byte())
        Dim drp As SqlCommand = New SqlCommand ("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 
