Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Saving and Calling a image from SQL Server DB

0.00/5 (No votes)
9 Mar 2010 1  
DAL Should like following when inserting a image. I used the Enterprise Library to data access. Data type of EmployerImage is image @ SQL Server

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

DAL Should like following when inserting a image. I used the Enterprise Library to data access. Data type of EmployerImage is image @ SQL Server table.

 

Public Sub Insert()
        Try
            Dim DB As Database = DatabaseFactory.CreateDatabase(CWB_DBCONNECTION_STRING)
            Dim DBC As DbCommand = DB.GetStoredProcCommand(EMPLOYERS_INSERT)
            DB.AddInParameter(DBC, "@EmployerID", DbType.Int64, EmployerID)         

            If EmployerImage Is Nothing Then
                DB.AddInParameter(DBC, "@EmployerImage", DbType.Binary, Nothing)
            Else
                DB.AddInParameter(DBC, "@EmployerImage", DbType.Binary, GetByteArrayFromImage(EmployerImage))

            End If

            DB.ExecuteNonQuery(DBC)
        Catch ex As Exception
            Throw
        End Try
 End Sub

 

Getting byte array from the image you need to create a function like following.

 Public Function GetByteArrayFromImage(ByVal img As Bitmap) As Byte()
        Try

            Dim ms As New System.IO.MemoryStream
            img.Save(ms, Imaging.ImageFormat.Bmp)
            Dim outBytes(CInt(ms.Length - 1)) As Byte
            ms.Seek(0, System.IO.SeekOrigin.Begin)
            ms.Read(outBytes, 0, CInt(ms.Length))
            Return outBytes
        Catch ex As Exception
            Return Nothing
            Throw

        End Try

    End Function

 

Calling the image from the SQL Server

 Public Sub SelectRow()
        Dim DB As Database = DatabaseFactory.CreateDatabase(CWB_DBCONNECTION_STRING)
        Dim DBC As DbCommand = DB.GetStoredProcCommand(EMPLOYERS_GETBYID)
        Try

            DB.AddInParameter(DBC, "@EmployerID", DbType.Int64, Me.EmployerID)
            Using DR As IDataReader = DB.ExecuteReader(DBC)

                With DR
                    Do While .Read
                                  
                        If Not IsDBNull(.Item("EmployerImage")) Then
                            EmployerImage = GetImageFromByteArray(.Item("EmployerImage"))
                        Else
                            EmployerImage = Nothing
                        End If                    


                    Loop
                End With

                If (Not DR Is Nothing) Then
                    DR.Close()
                End If
            End Using

        Catch ex As Exception
            Throw
        Finally
            DBC.Dispose()
        End Try
    End Sub

 

Getting saved image from SQL Server table

   Public Function GetImageFromByteArray(ByVal bytes As Byte()) As Bitmap
        Try
            Return CType(Bitmap.FromStream(New IO.MemoryStream(bytes)), Bitmap)
        Catch ex As Exception
            Return Nothing
            Throw
        End Try
    End Function

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here