Hey everybody :wave: ,
can anyone tell me How to store Image in SQL Server 2005 using Visual Basic 2005.
Printable View
Hey everybody :wave: ,
can anyone tell me How to store Image in SQL Server 2005 using Visual Basic 2005.
Create a MemoryStream, Save the Image to the stream, Seek to the beginning of the stream, then get the data from the stream into a Byte array. The Byte array is the object you save to the database. When retrieving the data you use the converse operaions. I've posted code to do this previously, so a search will turn it up for you.
This is straight out of 101 MSFT VB .Net Applications, so you might need to fiddle with it a bit but here goes:
(this is something i always want to try :) )
On Button Press Event
VB Code:
Dim ms as New MemoryStream() picSave.Image.Save(ms, picSave.Image.RawFormat) Dim arrImage() as Byte = ms.GetBuffer ms.Close() Dim strFileName as String = lblFilePath.text.Substring(lblFilePath.Text.LastIndexOf('|") + 1) Dim cnn as New SqlConnection(connectionString) Dim strSQL as String = _ "INSERT INTO Picture (Filename, Picture)" & _ "VALUES (@Filename, @Picture)" Dim cmd as NewSQLCommand(strSQL, cnn) With cmd .Parameters.Add(New SqlParameter("@Filename", _ SqlDbType.NVarChar, 50)).Value = strFileName .Parameters.Add(New SqlParameter("@Picture", _ SqlDbType.Image)).Value = arrImage End With cnn.Open() cmd.ExecuteNonQuery() cnn.Close
I hope this helps
When you create the table to store your pictures in SQL2005 declare the field to store images as Varbinary(MAX) instead of Image. :)
what is the Technical difference of storing images between varbinary(max) and binary ??? i am just curious :)
http://msdn2.microsoft.com/en-us/library/ms188362.aspxQuote:
Originally Posted by maged
I was going to ask why (as they are identical) but found the answer here:Quote:
Originally Posted by Shardox
http://msdn2.microsoft.com/en-us/library/ms187993.aspx
"ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead. For more information, see Using Large-Value Data Types."
Thank you for the Link :thumb: