Store image in SQL Database using vb.net


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

19 thoughts on “Store image in SQL Database using vb.net

  1. 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

  2. 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.

  3. 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

Leave a reply to sahir Afridi Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.