SQL 图片到 Crystal 报告
SQL Image to Crystal Report
我想从 SQL Server 2008 Express 中提取图像数据并直接在 Crystal 报告中显示。我正在使用 Visual Studio 2012 以及 Crystal Reports for Visual Studio(也是 SAP Crystal Reports 2013 的完整版本)。
我曾尝试使用 Google/SO 搜索中的样本,但我似乎至少遗漏了一个关键要素。在我的项目中,我有一个包含 System.Byte()
字段的数据集、一个 Crystal 报告和一个查看器。
这是查询数据库、读取内存流并将图像数据放入数据集中的代码片段。
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 查看器,但它是一个空白报告。我想知道我获取数据的顺序是否正确,以及我是否将其正确存储在数据集中。
经过更多 reading/researching 我得到了以下代码片段,它们似乎具有正确的顺序,并且正确地将数据存储在数据集中。此片段来自 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
结束函数
我想从 SQL Server 2008 Express 中提取图像数据并直接在 Crystal 报告中显示。我正在使用 Visual Studio 2012 以及 Crystal Reports for Visual Studio(也是 SAP Crystal Reports 2013 的完整版本)。
我曾尝试使用 Google/SO 搜索中的样本,但我似乎至少遗漏了一个关键要素。在我的项目中,我有一个包含 System.Byte()
字段的数据集、一个 Crystal 报告和一个查看器。
这是查询数据库、读取内存流并将图像数据放入数据集中的代码片段。
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 查看器,但它是一个空白报告。我想知道我获取数据的顺序是否正确,以及我是否将其正确存储在数据集中。
经过更多 reading/researching 我得到了以下代码片段,它们似乎具有正确的顺序,并且正确地将数据存储在数据集中。此片段来自 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
结束函数