Store image in SQL data base it is not a new thing in SQL and VB.net which I going to explain
To store the image in database we convert the image in bytes in vb.net and then it send to database to store.it will very simple step to store it.
Download :: http://www.4shared.com/file/VScQwina/image_in_sql_vbnet_2005_.html
1). Create a table called ImagesStore.
CREATE TABLE ImagesStore (ImageId int IDENTITY (1, 1) NOT NULL ,OriginalPath varchar (200) NOT NULL ,ImageData image NOT NULL)
2). Then the main code is written in vb.net . Bellow code is use to convert the image into byte and the send it to the database to store.
Convert the image and send it to database:
Dim str As String Dim fstream As FileStream Dim imgdata As Byte() Dim data As Byte() Dim finfo As FileInfo finfo = New FileInfo(TextBox3.Text) Dim numbyte As Long Dim br As BinaryReader numbyte = finfo.Length fstream = New FileStream(TextBox3.Text, FileMode.Open, FileAccess.Read) br = New BinaryReader(fstream) data = br.ReadBytes(numbyte) imgdata = data con = New SqlConnection(TextBox1.Text) con.Open() str = "insert into ImagesStore (OriginalPath,ImageData) values(@op, @i)" cmd = New SqlCommand(str, con) cmd.Parameters.Add(New SqlParameter("op", SqlDbType.Char, 200)) cmd.Parameters("op").Value = TextBox1.Text cmd.Parameters.Add(New SqlParameter("i", SqlDbType.Image)) cmd.Parameters("i").Value = imgdata cmd.ExecuteNonQuery() MsgBox("Image Inserted In DataBase..", MsgBoxStyle.Information) Me.Close() End Sub
3). to retrieve the image from the database and display in the Picture Box .below code is responsible for display the picture in picture box. The main function of code is to retrieve the data in bytes and convert it to a normal image and display it.
Dim imagedata As Byte() Try PictureBox1.Location = New Point(3, 3) Timer1.Enabled = False imagedata = (DataGridView1.Rows(e.RowIndex).Cells("ImageData").Value) Dim ms As MemoryStream ms = New MemoryStream(imagedata, 0, imagedata.Length) ms.Write(imagedata, 0, imagedata.Length) Dim im As Image im = Image.FromStream(ms, True) PictureBox1.Image = im Catch ex As Exception Timer1.Enabled = True End Try
hi!This was a really terrific blog!
I come from endland, I was luck to approach your Topics in yahoo
Also I obtain a lot in your blog really thank your very much i will come later
Hi bet365,
Thanks for your comment,and thanks for your approach,
Thank you.
hello sir i need a access data base for save a image in it with the help of vb.net
Hi,
you can download the sample code and customize it as your need.
http://www.4shared.com/file/VScQwina/image_in_sql_vbnet_2005_.html
this code is very helpful for my project
thanks
this code would help a lot in making our proposed system.. 😉
this is cooL! 🙂
Very simple and easy to use.
However, because you did not change the names of any of your controls you did not realise that Textbox1.Text was saved into my database instead of Textbox3.Text, Very Annoying.
Hi Andy,
I really thankful for your comment. Thanks for a good advice.
Hi…i want to ask you,how about if i want to save multiple image files ?thanks before
yes u can save multiple images .
thanks for the reply Mr. Kumar. but, how about the database field?it is enough if i just have one field to save the multiple images??
Hi youke,
yes for one filed is enough for storing the single image file for multiple images you need more then one filed just check the table structure or you can download the sample from this link.
Thank you Mr. Kumar, very helpful
Thanx alot Mr. Kumar …..
Many Many Thanks. It is Excellent.
What is the wrong in this code? getting an error at parameter ‘ms’… as parameter not valid.
Public Function ConvertBytes(ByRef myBytes As Byte()) As Image
Dim myimage As Image
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(myBytes)
myimage = Image.FromStream(ms)
Return myimage
End Function
please help me out
I think the admin of this website is genuinely working hard in favor of his site,
as here every information is quality based information.