SQL映像到Crystal报表

时间:2018-10-19 15:01:04

标签: sql-server visual-studio-2012 crystal-reports

我想从SQL Server 2008 Express中提取图像数据并将其直接显示在Crystal Report中。我正在使用Visual Studio 2012和Crystal Reports for Visual Studio(也是SAP Crystal Reports 2013的完整版)。

我尝试使用Google / SO搜索中的示例,但似乎缺少至少一个关键要素。在我的项目中,我有一个数据集,其中一个字段为System.Byte(),一个Crystal Report和一个查看器。

这是查询数据库,读取内存流并将图像数据放入数据集中的代码段。

 Dim ds As New DataSet1
   Dim row As DataRow

   Dim objConn As New SqlConnection(DatabaseConnection.CTLDataConnectionString)
   Dim objCommand As SqlCommand = objConn.CreateCommand
   objCommand.CommandText = "SELECT Content FROM Report WHERE HandlingUnitID = " & HandlingUID
   Dim dr As SqlDataReader = Nothing
   If objConn.State = ConnectionState.Closed Then objConn.Open()

   dr = objCommand.ExecuteReader

   If dr.HasRows Then
       dr.Read()
       Dim b() As Byte = DirectCast(dr("Content"), Byte())
       Using ms As New MemoryStream(b)
         ds.DataTable1.Rows.Add(b)
       End Using

   End If

   Dim rpt As New ShipLabel
   rpt.SetDataSource(ds.Tables("DataTable1"))

   Dim frm As New CRviewer
   frm.CRvwr.ReportSource = rpt
   frm.ShowDialog()

这将显示Crystal Viewer,但它是空白报告。我想知道是否有正确的顺序来获取数据,以及是否正确地将其存储在数据集中。

1 个答案:

答案 0 :(得分:0)

经过更多的阅读/研究之后,我得到了以下代码片段,这些片段似乎具有正确的顺序,并将数据正确存储在数据集中。该代码段来自cellclick事件。

 Dim ds As New DataSet1
   Dim row As DataRow

   Dim img As Bitmap

   Dim objConn As New SqlConnection(DatabaseConnection.CTLDataConnectionString)
   Dim objCommand As SqlCommand = objConn.CreateCommand
   objCommand.CommandText = "SELECT Content FROM Report WHERE HandlingUnitID = " & HandlingUID
   Dim dr As SqlDataReader = Nothing
   If objConn.State = ConnectionState.Closed Then objConn.Open()

   dr = objCommand.ExecuteReader

   If dr.HasRows Then
       dr.Read()
       Dim b() As Byte = dr("Content")

    Using ms As New MemoryStream(b)
        img = Image.FromStream(ms)
        b = ConvertImageToByte(img)
        ds.DataTable1.Rows.Add(b)
    End Using

   End If
   objConn.Close()

   Dim rpt As New ShipLabel
   rpt.SetDataSource(ds.Tables("DataTable1"))
   rpt.SetParameterValue("JobNumber", OrderNumber.ToString)

   Dim frm As New CRviewer
   frm.CRvwr.ReportSource = rpt
   frm.ShowDialog()

此功能:

Public Shared Function ConvertImageToByte(ByVal Value As Image) As Byte()
If Value IsNot Nothing Then
    Dim fs As MemoryStream = New MemoryStream()
    CType(Value, Bitmap).Save(fs, ImageFormat.Jpeg)
    Dim retval As Byte() = fs.ToArray()
    fs.Dispose()
    Return retval
End If

Return Nothing

结束功能