Net-informations.com
SiteMap  | About    

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.

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

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




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






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.