locked
Retreive image from database RRS feed

  • Question

  •  

    I just wanna to retrieve the image from SQL2005 Database using ADO.NET on windows form on clicking of button.
    Wednesday, September 12, 2007 10:26 AM

Answers

  • You can use the 'image' type in sql server 2005 to store the photo in the database. For retreving the picture and displaying it on a web page, you have to create a temporary file. Considering that your pictures are stored in a photo folder in the root folder and the column name is 'photo' from 'testTable', here is a sample code to get the job done :

     

                   

    Picture from DB

     

    Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionStringName").ConnectionString)
                        Const SQL As String = "SELECT [photo] FROM [TestTable] WHERE [userid] = @userid"

                        Dim myCommand As New SqlCommand(SQL, myConnection)
                        myCommand.Parameters.AddWithValue("@userid", user)
                        myConnection.Open()

                        Dim barrImg() As Byte = CType(myCommand.ExecuteScalar(), Byte())

                        Dim fs As FileStream = New FileStream(Server.MapPath("~\pictures\" + user), FileMode.CreateNew, FileAccess.Write)
                        fs.Write(barrImg, 0, barrImg.Length)
                        fs.Flush()
                        fs.Close()

                        Me.imgLogin.ImageUrl = "~/pictures/" + user

                        fs.Dispose()
                        myConnection.Close()
                    End Using

     

     

    Hope this solves your problem...

    Friday, September 14, 2007 11:49 AM

All replies

  • you have to do this thro transfering bytes of an image by reading it...
    Wednesday, September 12, 2007 10:51 AM
  • I think that this article should help you - http://support.microsoft.com/kb/321900

     

    Also please search for such things on the internet and only if you are not able to get results easily then post it on the forums.

    Friday, September 14, 2007 4:41 AM
  • You can use the 'image' type in sql server 2005 to store the photo in the database. For retreving the picture and displaying it on a web page, you have to create a temporary file. Considering that your pictures are stored in a photo folder in the root folder and the column name is 'photo' from 'testTable', here is a sample code to get the job done :

     

                   

    Picture from DB

     

    Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionStringName").ConnectionString)
                        Const SQL As String = "SELECT [photo] FROM [TestTable] WHERE [userid] = @userid"

                        Dim myCommand As New SqlCommand(SQL, myConnection)
                        myCommand.Parameters.AddWithValue("@userid", user)
                        myConnection.Open()

                        Dim barrImg() As Byte = CType(myCommand.ExecuteScalar(), Byte())

                        Dim fs As FileStream = New FileStream(Server.MapPath("~\pictures\" + user), FileMode.CreateNew, FileAccess.Write)
                        fs.Write(barrImg, 0, barrImg.Length)
                        fs.Flush()
                        fs.Close()

                        Me.imgLogin.ImageUrl = "~/pictures/" + user

                        fs.Dispose()
                        myConnection.Close()
                    End Using

     

     

    Hope this solves your problem...

    Friday, September 14, 2007 11:49 AM