none
how to retrieve an image from SQLSERVER 2005 using VB.NET? RRS feed

  • Question

  • I am able to store an image in SQL but cannot able to load the same image next time,
    Can anyone help me????????
    Please

    Thanking You
    Sunday, January 13, 2008 9:16 AM

Answers

All replies

  • Sunday, January 13, 2008 1:19 PM
    Moderator
  • wel i had the same problem during my project the solution provided by vmazur is excellent. i m providing my code

    create a database  table( with a column which has datatype as image)

    eg.

    create

     

    table emp (empid int, photo image)

    design a form with a textbox, picturebox,openfiledialog,browse button and submit button

    import the following

    Imports

     

    System.IO

    Imports

     

    System.Data.SqlClient

    code under browse button:

    OpenFileDialog1.Filter =

    "jpg Files(*.jpg)|*.jpg|Gif Files(*.gif)|*.gif|bmp Files(*.bmp)|*.bmp"

    OpenFileDialog1.ShowDialog()

    PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)

    code under submit button:

    Dim con As SqlConnection = New SqlConnection("User Id=sa;pwd=password; database= databasename")

    Try

     

    Dim st As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)

     

    Dim mbr As BinaryReader = New BinaryReader(st)

     

    Dim buffer(st.Length) As Byte

    mbr.Read(buffer, 0,

    CInt(st.Length))

    st.Close()

    con.Open()

     

    Dim Str As String = "insert into emp(empid,photo) values(" + TextBox1.Text + ",@photo)"

     

    Dim Cmd As New SqlCommand(Str, con)

    Cmd.Parameters.Add(

    "@photo", SqlDbType.Binary, buffer.Length).Value = buffer

    Cmd.ExecuteNonQuery()

    con.Close()

    MsgBox(

    "Record inserted Successfully")

     

    Catch ex As Exception

    con.Close()

    MsgBox(

    "Error In Inserting record", MsgBoxStyle.Critical, "Error")

    MsgBox(ex.ToString)

     

    End Try

     

    if u still have any problem mail me on sajid_kazi90@rediffmail.com

    Friday, October 15, 2010 8:19 PM