Saving Image to database

D

Dav

Hi all,

I am using these code to save Image to database:
Dim fs As FileStream = New FileStream(filename, _
FileMode.OpenOrCreate, FileAccess.Read)
Dim rawData() As Byte = New Byte(fs.Length) {}
fs.Read(rawData, 0, System.Convert.ToInt32
(fs.Length))
fs.Close()

OleDbDataAdapter1.InsertCommand.CommandText = _
"Insert Into ImageTest Values (" & rawData & ")"
OleDbDataAdapter1.InsertCommand.ExecuteNonQuery()
MsgBox("Image saved to database")

But there is a problem in :
"Insert Into ImageTest Values (" & rawData & ")"

The error message is
Operator '&' is not defined for types 'String' and '1-
dimensional array of Byte'.

Anyone can help
Thanks
 
P

Peter Proost

Hi maybe this piece of code can help you

Dim ms As New System.IO.MemoryStream()
Dim arrImage() As Byte
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat.Jpeg)
arrImage = ms.GetBuffer()
ms.Close()
Dim updCommand As SqlCommand
Try
updCommand = New SqlCommand("Update table set bitmap= @bitmap where
code=@code)
updCommand.Parameters.Add(New SqlParameter("@bitmap", SqlDbType.Image))
updCommand.Parameters.Add(New SqlParameter("@code", SqlDbType.Char))
updCommand.Parameters("@code").Value = 'Your code field
if arrImage Is Nothing Then
updCommand.Parameters("@bitmap").Value = DBNull.Value
Else
updCommand.Parameters("@bitmap").Value = arrImage
End If
updCommand.Connection = 'Your connection
updCommand.Connection.Open()
updCommand.ExecuteNonQuery()
updCommand.Connection.Close()

Catch ex As Exception
MsgBox(ex.tostring)
end try
 
C

Cor Ligthert

Herfried,

Did you look at the problem?
I cannot see what your links has to do with it.
Maybe can you edify me.

The problem is inserting in a database.

Cor


"Herfried K. Wagner [MVP]"
 
C

Cor Ligthert

Dav,

I don't see you use a dataset, so probably is this an answer what maybe can
be the solution for your problem.

\\\
Dim strSQL As String = "INSERT INTO imageTest (MyPictureField) VALUES
(@MyParam)"
Dim cmd As New OleDbCommand(strSQL, MyConnection)
cmd.Parameters.Add("@MyParam", rawData)
Conn.Open()
cmd.ExecuteNonQuery()
///
I did not test it, however it should be something as this,

I hope it helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top