You can stream the binary data returned by SQL Server directly using the
Response.BinaryWrite
method. Do this after retrieving the data from the database.
In <script runat="server" /> block or codebehind:
Sub Page_Load(sender As Object, e As EventArgs)
Dim connectionString as String = "Your connection string"
Dim objConnection as New SqlConnection(connectionString)
Dim commandText as String = "SELECT Image FROM imageTable"
Dim objCommand as New SqlCommand(commandText, objConnection)
Dim objReader as SqlDataReader
Try
objConnection.Open()
objReader = objCommand.ExecuteReader()
If objReader.Read() Then
Response.ContentType = "image/jpeg"
Response.BinaryWrite( CType(objReader("Image"), Byte()) )
End If
Catch exc as Exception
Response.Write( exc.ToString() )
Finally
If Not objConnection Is Nothing Then
If objConnection.State = ConnectionState.Open Then
objConnection.Close()
End If
End If
If Not objReader Is Nothing Then
objReader.Close()
End If
End Try
End Sub
Comments
This code will display the contents of the first record in the imageTable. In the example
code, the Image field in the SQL Server contains the binary data that is written out with
the Response.BinaryWrite method.
Response.ContentType should be set to the appropriate MIME-type of the image
that you are trying to display.This can be stored in the database as well and retrieved
along with the image data.
No comments:
Post a Comment