Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB.NET
|
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 6th, 2005, 02:21 PM
Registered User
 
Join Date: Jul 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mojo
Default How to binding picture file in database ?

I stuck at this thing about 1-2 weeks and up until now I cant figure it out.
Please help me, thanks very much.

 
Old September 7th, 2005, 06:49 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi,

I have had to do exactly this before. The main bits of my code are below. I hope the syntax is right, as I converted it from c# and im not too experienced in vb...

To insert an Image object into a column, picture, we put the object into an intermediate MemoryStream, then read an array of bytes from it, as that is how it is stored in the database. For an Image myImage:

' save to intermediate MemoryStream
Dim ms As New MemoryStream()
myImage.Save(ms, myImage.RawFormat)

' read byte array from stream
Dim bytBLOB() As New Byte(ms.Length)
ms.Position = 0
ms.Read(bytBLOB, 0, Convert.ToInt32(ms.Length))

' create parameter to insert
Dim parImage As New SqlParameter("@picture", SqlDbType.VarBinary)
parImage.Size = ms.Length
parImage.Value = bytBLOB

' we can now use the parameter in an SqlCommand etc as ususal

To retrieve the image using a SqlDataReader, we reverse the process, but it is much easier:

' read the byte array from the data reader
bytBLOBData = CType(sqldatardr.Item("picture"), Byte());
' create another MemoryStream
Dim msBLOBData As New MemoryStream(bytBLOBData);
' read Image object from MemoryStream
Image myImage = System.Drawing.Image.FromStream(msmBLOBData);

Another method can be found at
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp

Hope this helps
Philip Cole





Similar Threads
Thread Thread Starter Forum Replies Last Post
Display picture from database using JSP codes ban_gal Java Databases 6 May 30th, 2008 08:21 PM
trying to bind a picture box to an access database dups General .NET 0 July 20th, 2006 12:56 AM
How to save a picture in Microsoft Access Database Edward King Java Databases 0 January 4th, 2006 01:27 AM
retriving picture from database(sql) to <asp:image aspnet79434319 Classic ASP Basics 0 June 21st, 2005 01:29 AM
Inserting a picture from a database into Word fastcorvette Pro VB Databases 1 February 15th, 2005 07:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.