Click here to Skip to main content
15,867,568 members
Articles / Database Development
Article

Saving and Calling a image from SQL Server DB

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 6.7K   2  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --