Retrieve image from database in VB.NET

Managed Providers, within the .NET Framework, refer to a cohesive set of classes that serve as the foundation for the ADO.NET programming model. These classes are specifically designed to facilitate efficient and consistent data access and manipulation across various data sources.

In the scope of data storage and representation, a data type is an attribute that defines the specific type of data that an object can hold. It categorizes data into various classifications, such as integer data, character data, date and time data, image data, and so on. This classification ensures proper storage, retrieval, and manipulation of data based on its inherent nature.

Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte())

Image data type

In the case of the image data type, conversion to another data type is not supported, either implicitly or explicitly. This means that direct conversion of image data to a different data type is not possible within the constraints of the image data type itself. However, it is important to note that indirect conversion of image data can still be achieved through appropriate techniques and methods.

While direct conversion may not be feasible, developers can employ alternative approaches to handle image data. For example, they can manipulate the image data indirectly by extracting specific attributes or characteristics of the image, converting it to a suitable format for further processing, or using additional techniques to modify or analyze the image indirectly.

The following VB.NET source code shows how to retrieve an image from SQL Server.

Full Source VB.NET
Imports System.IO Imports System.Data.SqlClient Public Class Form1 Dim cnn As SqlConnection Dim connectionString As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click connectionString = "Data Source=servername; Initial Catalog=databasename; User ID=sa; Password=password" cnn = New SqlConnection(connectionString) Dim stream As New MemoryStream() cnn.Open() Dim command As New SqlCommand("select img from imgtable where id=1", cnn) Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte()) stream.Write(image, 0, image.Length) cnn.Close() Dim bitmap As New Bitmap(stream) PictureBox1.Image = bitmap End Sub End Class