How To Retrieve Images From sql2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mafaisal
    New Member
    • Sep 2007
    • 142

    How To Retrieve Images From sql2005

    Hello Experts

    how to retrieve images From sql2005 to vb.net2005

    Also i insert image into database using stored procedures, Is any other way to insert in vb.net2005

    Faisal
    Last edited by mafaisal; Mar 7 '08, 06:51 PM. Reason: Spelling mistake
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    What you have stored in databbase ?

    The path of the file or the image itself ?

    Comment

    • mafaisal
      New Member
      • Sep 2007
      • 142

      #3
      Hello
      Thanx For Reply
      I stores image itself
      Bcoz Path is not Proper
      i store Company Logo
      That is Diff in Different client so I store image itself

      But Problem is Hw that is Retrieve and show in Crystal report

      Expecting Reply

      Faisal

      Originally posted by debasisdas
      What you have stored in databbase ?

      The path of the file or the image itself ?

      Comment

      • Prathap
        New Member
        • Nov 2011
        • 37

        #4
        Solved
        Code:
        Imports System.Data.SqlClient
        Imports System.Data
        Imports System.IO
         
         Public Class Form1
            Dim str As String = "Data Source=NET3\SQLEXPRESS;Initial Catalog=RestPos;Persist Security Info=True;User ID=sa;Password=password"
            Dim con As New SqlClient.SqlConnection
        
            'To open an image from computer
            '-------------------------------
            Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                OpenFileDialog1.Title = "Please select a file"
                OpenFileDialog1.InitialDirectory = "c:temp"
                OpenFileDialog1.ShowDialog()
                TextBox2.Text = OpenFileDialog1.FileName.ToString  '--->To Show the file path in textbox2
                PictureBox1.ImageLocation = TextBox2.Text  '--->To show selected image in picturebox
            End sub
           
           'To insert selected image into database
           'The datatype of the column in table to store image should be <image>  
           '--------------------------------------------------------------------
            Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
                con.ConnectionString = str
                Dim ms As New IO.MemoryStream()
                PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
                Dim arrimage() As Byte = ms.GetBuffer
                Dim cmd As New SqlCommand("insert into image (Emp_Image)values(@picture)", con)
                cmd.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = arrimage
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
            End Sub
            
            'We have successfully inserted the image into database.
            'Now we want to Retrieve the image from database.
            '-------------------------------------------------
            Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
                Dim stream As New IO.MemoryStream()
                con.Open()
                Dim command As New SqlCommand("select Emp_Image from Image where Emp_Id='" + TextBox3.Text + "'", con) '--->You can give Emp_id instead of Textbox value.
                Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte())
                stream.Write(image, 0, image.Length)
                con.Close()
                Dim bitmap As New Bitmap(stream)
                PictureBox2.Image = bitmap '--->I have used another picturebox to display image from database.
            End Sub
            'Thats all, You can place a linklabel below the picturebox if you to change photo and update it in database.
        Regards,
        Prathap

        Comment

        Working...