Pictures

SQL CE and Compact Framework programming with Visual Basic .NET

Pictures

Postby Carmen » Thu Nov 26, 2009 5:25 pm

can you show me the source code to add pictures to a database?
Carmen
 
Posts: 1
Joined: Thu Nov 26, 2009 5:23 pm

Re: Pictures

Postby 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
User avatar
capthow
Site Admin
 
Posts: 7
Joined: Thu Nov 05, 2009 2:52 pm


Return to VB Net

Who is online

Users browsing this forum: No registered users and 1 guest

cron