VB.net and SQL Server

VB.net and SQL Server

Post by AJ » Wed, 11 Oct 2006 14:49:30



Hi
I am new to this area. I need some help regarding storing images in to
an SQL database and retrieving that image back to a picture box control
using a query.

I used openfiledialog to select the image
code i used as follows, its storing images as binary form, but i am not
sure whether its saving the path of that image or the image itself.
-------------------------
OpenFileDialog1.ShowDialog()
        'Dim MySource1 = OpenFileDialog1.FileName
        'myComm1 = New OleDb.OleDbCommand("insert into Image (studid,
Img) values ('04-00001', '" & MySource1 & "')", myConn)
        'dr3 = myComm1.ExecuteReader
-----------------

Can anyone help me to build this code..

 
 
 

VB.net and SQL Server

Post by Jamie Frase » Thu, 12 Oct 2006 04:56:07


You are just storing the actual filename (i.e. a string).

What you want to do is;

1) Use a streamreader to open the file and read its contents into a
byte array (depending on your .NET version you can do this one of
several ways, .net 2.0 you can do something like
my.computer.filesystem.readallbytes)

2) Store this byte array into your DB; field type "image".

3) When reading the row back out, you need to create a stream (I
generally use a memorystream) from your "image" field in the DB, then
create an image using Image.FromStream. Then you can just use this
Image as the source of your picturebox control.

That should point you in the right direction :)

-Jamie


Quote:

> Hi
> I am new to this area. I need some help regarding storing images in to
> an SQL database and retrieving that image back to a picture box control
> using a query.

> I used openfiledialog to select the image
> code i used as follows, its storing images as binary form, but i am not
> sure whether its saving the path of that image or the image itself.
> -------------------------
> OpenFileDialog1.ShowDialog()
>         'Dim MySource1 = OpenFileDialog1.FileName
>         'myComm1 = New OleDb.OleDbCommand("insert into Image (studid,
> Img) values ('04-00001', '" & MySource1 & "')", myConn)
>         'dr3 = myComm1.ExecuteReader
> -----------------

> Can anyone help me to build this code..