bilko73
Oct 8th, 2002, 12:52 PM
does anyone know how to update an image from vb.net to a sql server 2000 table?
thanks
Edneeis
Oct 8th, 2002, 12:59 PM
Do you mean store the image in the database?
Public Sub ImportImageToDB(ByVal dr As DataRow, ByVal Field As String, ByVal Img As Image)
'---------------------------------------------------------------------------------------
' Author : Eddiem
' Purpose : Writes an unknown file type to a database.
' Syntax : ImportImgagToDB(dr,"Picture",PictureBox1.Image)
'---------------------------------------------------------------------------------------
Dim ms As MemoryStream = New MemoryStream()
Img.Save(ms, Img.RawFormat)
Dim data(ms.Length) As Byte
ms.Position = 0
ms.Read(data, 0, ms.Length)
dr.Item(Field) = data
ms.Close()
End Sub
Public Function ExportImageFromDB(ByVal dr As DataRow, ByVal Field As String) As Image
'---------------------------------------------------------------------------------------
' Author : Eddiem
' Purpose : Gets an Image from a database.
'---------------------------------------------------------------------------------------
Dim ms As MemoryStream = New MemoryStream()
Dim data() As Byte
Dim img As Image
Try
data = CType(dr.Item(Field), Byte())
ms.Write(data, 0, data.GetUpperBound(0))
img = Image.FromStream(ms)
Catch ex As Exception
'handle any errors other than no picture set in db yet
If Not TypeOf ex Is ArgumentOutOfRangeException Then
Throw ex
End If
Finally
ms.Close()
ms = Nothing
End Try
Return img
End Function
Public Function ExportImageFromDB(ByVal dr As SqlDataReader, ByVal Field As String) As Image
'---------------------------------------------------------------------------------------
' Author : Eddiem
' Purpose : Gets an Image from a database.
'---------------------------------------------------------------------------------------
Dim ms As MemoryStream = New MemoryStream()
Dim data() As Byte
Dim img As Image
Try
data = CType(dr.Item(Field), Byte())
ms.Write(data, 0, data.GetUpperBound(0))
img = Image.FromStream(ms)
Catch ex As Exception
'handle any errors other than no picture in db
If Not TypeOf ex Is ArgumentOutOfRangeException Then
Throw ex
End If
Finally
ms.Close()
ms = Nothing
End Try
Return img
End Function