473,320 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Saving Image to database

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
5 6336
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
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
2
by: Brad | last post by:
I have code which takes an image, uploaded from a web page, and saves it to a database. Now I want to always resize an uploaded image before it is saved to the database. My code to resize is...
12
by: tjonsek | last post by:
I get a generic error (not very helpful) when attempting to save a re-sized image back to its original location. Here is the code snippet: Dim g As System.Drawing.Image =...
5
by: Mark | last post by:
I have a VB 6 program that I have saved an audio file to using DAO and the appendchunk as is shown at http://support.microsoft.com/kb/103257/EN-US/. ; I have it working fine in that app (it saves...
6
by: Jeff | last post by:
Hey (and thank you for reading my post) In visual web developer 2005 express edition I've created a simple website project.. At this website I want users who register to be able to upload a...
6
by: Mark Denardo | last post by:
My question is similar to one someone posted a few months back, but I don't see any replies. Basically I want to be able to have users upload photos and save them in a database (as byte data)...
2
by: tomlebold | last post by:
It takes five minutes when using object linking and embedding to save a PDF file using a normal from. The data type is image in an SQL Sever table. We are trying to keep track of the legal...
3
by: mirianCalin | last post by:
the code saves the category, image title, image, and feature.. but the problem is that the "feature" is not saved, but the others were saved.. this is the data types of my table category = text...
2
by: keerthisreenu | last post by:
hai to all...!! iam working with Ms Access 2000. iam saving an image in the database. After that i want to retrive the same image from database and displaying it in the picture box. but its not...
10
by: Nathan Sokalski | last post by:
I am using ASP.NET 2.0, and need to know how to save and use an image that is stored in an SQL Server image datatype. How can I do this using ASP.NET? Thanks.
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.