Winforms N-Tier - 从sql server数据库中检索并显示图像

时间:2014-06-30 09:06:30

标签: sql-server image winforms n-tier-architecture

N-Tier编程的新手,在学习更多内容的过程中,我想使用N层架构从SQL Server数据库中检索图像。我已设法保存图像但面临从数据库中获取图像,将其作为参数传递然后显示图像的问题。

数据访问层代码:

Public Function SelectRecord(ByVal clsdbo_ProductsPara As AppName.BusinessLogic.BLL_Products) As AppName.BusinessLogic.BLL_Products
    Dim clsDAL_Products As New AppName.BusinessLogic.BLL_Products
    Dim connTemp As SqlConnection = AppName.DataAccess.AppNameDataClass.GetConnection
    Dim strSelect As String = "SELECT * FROM [dbo].[Products] WHERE [ProductID] = @ProductID"
    Dim cmdSelect As New SqlCommand(strSelect, connTemp)
    cmdSelect.CommandType = CommandType.Text
    cmdSelect.Parameters.AddWithValue("@ProductID", clsdbo_ProductsPara.ProductID)
    Try
        connTemp.Open()
        Dim rdrTemp As SqlDataReader = strCommandSelect.ExecuteReader(CommandBehavior.SingleRow)
        If rdrTemp.Read Then
            With clsdbo_ProductsPara
                .ProductID = System.Convert.ToString(rdrTemp("ProductID"))
                .ProductDesc = If(IsDBNull(rdrTemp("ProductDesc")), Nothing, rdrTemp("ProductDesc"))
                .ProductImage = If(IsDBNull(rdrTemp("ProductImage")), Nothing, rdrTemp("ProductImage"))
            End With
        Else
            clsdbo_ProductsPara = Nothing
        End If
    Catch ex As SqlException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
    Finally
        connTemp.Close()
    End Try
    Return clsdbo_ProductsPara
End Function

表示层代码:

Private Sub DisplaySelectedRecordDetails()
    Dim clsdbo_ProductsPara As New AppName.BusinessLogic.BLL_Products
    clsdbo_ProductsPara.ProductID = System.Convert.ToString(lblProductID.Text)
    clsdbo_ProductsPara = clsdbo_ProductsData.SelectRecord(clsdbo_ProductsPara)
    If Not clsdbo_ProductsPara Is Nothing Then
        Try
            txtProductDesc.Text = Convert.ToString(clsdbo_ProductsPara.ProductDesc)
            picProductImage.Image = Nothing
            If Not IsDBNull(clsdbo_ProductsPara.ProductImage) Then
                Dim data As Byte = Convert.ToByte(clsdbo_ProductsPara.ProductImage.GetType)
                Dim bw As New BinaryWriter(New MemoryStream)
                bw.Write(clsdbo_ProductsPara.ProductImage)
                Dim img As Image = Image.FromStream(bw.BaseStream, True)
                Me.picProductImage.Image = img
            End If
        Catch
        End Try
    End If
End Sub

首先,我想知道使用此方法检索图像的正确方法是什么。其次我收到转换错误从'Byte()'类型到'Byte'类型的转换无效。在该行的DAL块中.ProductImage = If(IsDBNull(rdrTemp(“ProductImage”)),Nothing,rdrTemp(“ProductImage”))

请帮助我纠正以上内容,让我明白。

谢谢

CL

0 个答案:

没有答案