VB.Net从数据库到图片框打开图像

时间:2014-08-18 12:40:12

标签: database vb.net

我正在尝试从数据库打开图像到图片框,但我不知道如何操作。 我已经搜索了一些答案,我不熟悉我只是初学者的代码。

我研究过的唯一代码是将数据库连接到系统:

    Imports System.Data.OleDb
Module Module1
    Public acsconn As New OleDbConnection
    Public acsdr As OleDbDataReader
    Public acsda As New OleDbDataAdapter
    Public acscmd As New OleDbCommand
    Public strsql As String
    Public acsds As New DataSet
    Public Sub connect()
        Try
            acsconn.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=|datadirectory|\database1.mdb;"
            acsconn.Open()
            If acsconn.State = ConnectionState.Open Then
                MsgBox("Connected")
            Else
                MsgBox("Error")
            End If
        Catch ex As Exception


        End Try
    End Sub
End Module

我不知道下一步是什么。顺便说一句,这些代码 - 我用它来保存图像到数据库。

1 个答案:

答案 0 :(得分:1)

我认为这是你要找的东西:

 Private Sub HandleRequest(context as HttpContext)
      Dim SqlCnn As SqlConnection = Nothing, sql As String
      Dim emp_id As Integer
      emp_id = Int32.Parse(context.Request.QueryString("id"))
      ConnectDB(SqlCnn)
      Try
        sql = "SELECT image FROM employees (NOLOCK) WHERE ID =" & emp_id
        sqlcmd = New SqlCommand(sqlstr, SqlCnn)
        Dim imageData As Byte() = DirectCast(sqlcmd.ExecuteScalar(), Byte())
        context.Response.ContentType = "image/jpeg"
        context.Response.BinaryWrite(imageData)
      Catch ex As Exception
        ReportError(ex)
      Finally
        CloseDB(SqlCnn)
      End Try
    End Sub