Answered by:
Retreive image from database

Question
-
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 DBUsing 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 UsingHope this solves your problem...
- Marked as answer by Vishal_Gupta_bwr Friday, January 6, 2012 10:22 AM
Friday, September 14, 2007 11:49 AM
All replies
-
you have to do this thro transfering bytes of an image by reading it...
- Marked as answer by Vishal_Gupta_bwr Friday, January 6, 2012 10:23 AM
- Unmarked as answer by Vishal_Gupta_bwr Friday, January 6, 2012 10:23 AM
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.
- Unmarked as answer by Vishal_Gupta_bwr Friday, January 6, 2012 10:23 AM
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 DBUsing 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 UsingHope this solves your problem...
- Marked as answer by Vishal_Gupta_bwr Friday, January 6, 2012 10:22 AM
Friday, September 14, 2007 11:49 AM