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.

01Public Function ScalarToImage(ByRef _SqlConnection As System.Data.SqlClient.SqlConnection, ByVal _SQL As String) As System.Drawing.Image
02    Dim _SqlRetVal As Object = Nothing
03    Dim _Image As System.Drawing.Image = Nothing
04 
05    Try
06        ' Executes the query, and returns the first column of the first row in the result
07        ' set returned by the query. Additional columns or rows are ignored.
08        Dim _SqlCommand As New System.Data.SqlClient.SqlCommand(_SQL, _SqlConnection)
09 
10        _SqlRetVal = _SqlCommand.ExecuteScalar()
11 
12        ' Dispose command
13        _SqlCommand.Dispose()
14        _SqlCommand = Nothing
15    Catch _Exception As Exception
16        ' Error occurred while trying to execute reader
17        ' send error message to console (change below line to customize error handling)
18        Console.WriteLine(_Exception.Message)
19 
20        Return Nothing
21    End Try
22 
23    ' convert object to image
24    Try
25        ' get image from object
26        Dim _ImageData(-1) As Byte
27        _ImageData = CType(_SqlRetVal, Byte())
28        Dim _MemoryStream As New System.IO.MemoryStream(_ImageData)
29        _Image = System.Drawing.Image.FromStream(_MemoryStream)
30    Catch _Exception As Exception
31        ' Error occurred while trying to create image
32        ' send error message to console (change below line to customize error handling)
33        Console.WriteLine(_Exception.Message)
34 
35        Return Nothing
36    End Try
37 
38    Return _Image
39End 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.

01' set temporary variable for database connection
02Dim _SqlConnection As New System.Data.SqlClient.SqlConnection()
03 
04' assign database connection string
05_SqlConnection.ConnectionString = "Server=SERVERADDRESS;Database=DATABASENAME;Uid=USERID;Pwd=PASSWORD;"
06 
07' Connect to database
08Try
09    _SqlConnection.Open()
10Catch _Exception As Exception
11    ' Error occurred while trying to connect to database
12    Console.WriteLine(_Exception.Message)
13End Try
14 
15' Check for valid open database connection before query database
16If _SqlConnection IsNot Nothing AndAlso _SqlConnection.State = ConnectionState.Open Then
17    ' get image data from database
18        ' Pass open database connection to function
19        ' Pass SQL statement get image data
20    Dim _Image As System.Drawing.Image = ScalarToImage(_SqlConnection, "SELECT image FROM sampletable WHERE productid = 14")
21 
22    If _Image IsNot Nothing Then
23        ' Lets show this image
24        pictureBox1.Image = _Image
25    Else
26        ' Failed to get image data from database
27        pictureBox1.Image = Nothing
28    End If
29 
30    ' close database connection
31    _SqlConnection.Close();
32End 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

 Poster Information 


(will not be published, required)

 MESSAGE DETAILS