Retrieve image from database
Managed Providers are a collection of classes in the .NET Framework that provide a foundation for the ADO.NET programming model. A data type is an attribute that specifies the type of data that the object can hold: integer data, character data, date and time data, image data and so on.
Conversion of image data type to another data type is not supported, implicitly or explicitly. However, indirect conversion of image data can be performed.
The following VB.NET source code shows how to retrieve an image from SQL Server.
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()
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)
Dim bitmap As New Bitmap(stream)
PictureBox1.Image = bitmap