Visual Basic Code Snippet - Get Image from sql server

Visual Basic Code Snippet - Get Image from sql server

Visual Basic Code Snippet - Get Image from sql server

(VB) Visual Basic code snippet connects to SQL server and executes SQL statement and returns the Image data. ScalarToImage returns an Image using open database connection and SQL statement.

Bookmark:

Visual Basic Code Snippet - Get Image from sql server

This .Net Visual Basic code snippet connects to SQL server and executes SQL statement and returns the binary image data from database. To use this function simply provide open database connection and SQL statement. This function uses SqlClient name space to get data using SqlCommand. Modify the exception handling section to as your project requirements.

Public Function ScalarToImage(ByRef _SqlConnection As System.Data.SqlClient.SqlConnection, ByVal _SQL As String) As System.Drawing.Image
    Dim _SqlRetVal As Object = Nothing
    Dim _Image As System.Drawing.Image = Nothing

    Try
        ' Executes the query, and returns the first column of the first row in the result 
        ' set returned by the query. Additional columns or rows are ignored.
        Dim _SqlCommand As New System.Data.SqlClient.SqlCommand(_SQL, _SqlConnection)

        _SqlRetVal = _SqlCommand.ExecuteScalar()

        ' Dispose command
        _SqlCommand.Dispose()
        _SqlCommand = Nothing
    Catch _Exception As Exception
        ' Error occurred while trying to execute reader
        ' send error message to console (change below line to customize error handling)
        Console.WriteLine(_Exception.Message)

        Return Nothing
    End Try

    ' convert object to image
    Try
        ' get image from object
        Dim _ImageData(-1) As Byte
        _ImageData = CType(_SqlRetVal, Byte())
        Dim _MemoryStream As New System.IO.MemoryStream(_ImageData)
        _Image = System.Drawing.Image.FromStream(_MemoryStream)
    Catch _Exception As Exception
        ' Error occurred while trying to create image
        ' send error message to console (change below line to customize error handling)
        Console.WriteLine(_Exception.Message)

        Return Nothing
    End Try

    Return _Image
End Function


Here is a simple example showing how to use above function (ScalarToImage) to connect to SQL database and get image data and show it in a picturebox.

' set temporary variable for database connection
Dim _SqlConnection As New System.Data.SqlClient.SqlConnection()

' assign database connection string
_SqlConnection.ConnectionString = "Server=SERVERADDRESS;Database=DATABASENAME;Uid=USERID;Pwd=PASSWORD;"

' Connect to database
Try
    _SqlConnection.Open()
Catch _Exception As Exception
    ' Error occurred while trying to connect to database
    Console.WriteLine(_Exception.Message)
End Try

' Check for valid open database connection before query database
If _SqlConnection IsNot Nothing AndAlso _SqlConnection.State = ConnectionState.Open Then
    ' get image data from database
        ' Pass open database connection to function
        ' Pass SQL statement get image data
    Dim _Image As System.Drawing.Image = ScalarToImage(_SqlConnection, "SELECT image FROM sampletable WHERE productid = 14")

    If _Image IsNot Nothing Then
        ' Lets show this image
        pictureBox1.Image = _Image
    Else
        ' Failed to get image data from database
        pictureBox1.Image = Nothing
    End If

    ' close database connection
    _SqlConnection.Close();
End If


VB Keywords Used:

  • Byte
  • CType
  • Image
  • MemoryStream
  • FromStream
  • ExecuteScalar
  • SqlConnection
  • ConnectionString
  • SqlCommand
  • Exception

Code Snippet Information:

  • Applies To: .Net, VB, Visual Basic, CLI, SQL, SqlCommand, ExecuteScalar, MemoryStream, Image, SQL Server, SQL Client, Connection String, Database Connection, SQL Server Binary Data
  • Programming Language : Visual Basic (VB)

External Resources:

Leave a comment