by capthow » Tue Dec 01, 2009 4:26 pm
'The following will load a picture (c:\test.jpg) into an SQLCE database (Database = c:\test.sdf, Table = MyTable, Column = Picture, Type=image)
'set up connection
Dim MyConnection As New SqlServerCe.SqlCeConnection()
MyConnection.ConnectionString = "Data Source='c:\test.sdf';"
'Get name of picture file
Dim FileName As String = "c:\test.jpg"
'Open file and read into a file byte array
Using FS As New FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read)
Using SR As New StreamReader(FS)
Dim FileByteArray(FS.Length - 1) As Byte
FS.Read(FileByteArray, 0, FS.Length)
' set command to add new picture to database
Dim Sql As String = "INSERT INTO MyTable(Picture) VALUES (@PicStream)"
Dim SqlCom As New SqlServerCe.SqlCeCommand(Sql, MyConnection)
'Add the parameters
SqlCom.Parameters.Add("@PicStream", SqlDbType.Image).Value = FileByteArray
'Exectute the command
MyConnection.Open()
SqlCom.ExecuteNonQuery()
MyConnection.Close()
End Using
End Using