By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

Saving Image to database

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
"Dav" <an*******@discussions.microsoft.com> wrote in message
news:0d****************************@phx.gbl...
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

Nov 21 '05 #2

P: n/a
"Dav" <an*******@discussions.microsoft.com> schrieb:
I am using these code to save Image to database:


If you store the images as a BLOB:

HOW TO: Read and Write a File to and from a BLOB Column by Using ADO.NET and
Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;316887>

HOW TO: Read and Write a File to and from a BLOB Column by Using Chunking in
ADO.NET and Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;317034>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3

P: n/a
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]"
"Dav" <an*******@discussions.microsoft.com> schrieb:
I am using these code to save Image to database:


If you store the images as a BLOB:

HOW TO: Read and Write a File to and from a BLOB Column by Using ADO.NET
and
Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;316887>

HOW TO: Read and Write a File to and from a BLOB Column by Using Chunking
in
ADO.NET and Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;317034>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4

P: n/a
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

"Dav" <an*******@discussions.microsoft.com>
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

Nov 21 '05 #5

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
The problem is inserting in a database.


I know that.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.