2016-12-01 82 views
0

我如何在没有图像路径的水晶报告中显示图像。 我有ID(int)和Pic(图像)的数据库卡。以编程方式将图像添加到Crystall报告

我添加数据表到水晶报表dtCard与列= ID类型=字符串 PIC类型=字节()

这里我的代码显示报告:

======== ========================================================

Dim report As New reportCard 

Dim path As String = Application.StartupPath & "\docReportCard.rpt" 

report.Load(path) 

Dim dt As New DataTable("dtCard") 

dt.Columns.Add("Id") 
dt.Columns.Add("pic") 

// dtData return value from database 

Dim row As DataRow = dt.NewRow 
row("Id") = dtData.Rows(0).Item("id") 
row("pic") = DirectCast(dtData.Rows(0).Item("pic"), Byte()) 

dt.Rows.Add(row) 

report.Database.Tables("dtCard").SetDataSource(dt) 
CrystalReportViewer1.ReportSource = report 

======= ===========================================

当我运行代码,没有任何错误,但图像无法显示。 Crystal报告中只有“System.Byte()”。

我该如何解决这个问题? 我想从我的水晶报告中显示数据库的Pic。

最好的问候, surbakti

+0

在你的模式t他的图像列的数据类型应该是

+0

这里代码来自我的模式:。但仍然不显示图像。任何想法? – Surbakti

+0

如果您在设计时将模式设置为报表的数据源,则图像列的数据类型应为IBlobFieldObject –

回答

0

1种形式和1个CrystalReport创建一个新项目。该表单应该有1个按钮和1个CrystalReportViewer控件。将此代码粘贴到窗体的代码窗口中。

Public Class Form1 
Dim table As New DataTable("test") 
Dim column As DataColumn 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    table = New DataTable("test") 
    With table 
     column = New DataColumn 
     With column 
      .ColumnName = "id" 
      .DataType = GetType(Integer) 
      .AutoIncrement = True 
      .AutoIncrementSeed = 0 
      .AutoIncrementStep = 1 
     End With 
     .Columns.Add(column) 

     column = New DataColumn 
     With column 
      .ColumnName = "pic" 
      .DataType = GetType(Byte())     
     End With 
     .Columns.Add(column) 
    End With 

    ' create XSD file for report datasource 
    ' uncomment this line on first run to create the datasource schema of your crystal report document. 
    ' Once schema is created, point your crystal report document's datasource to its location 
    ' After setting the data source, when you drag the "pic" column, its datatype will be IBlobFieldObject. 
    'table.WriteXmlSchema("E:\TMP\TestReport.XSD") 

    ' uncomment the following on succeeding runs, i.e. after setting the data source in the crystal report file 
    'ShowReport() 
End Sub 






Private Sub ShowReport() 
    Dim row As DataRow 
    row = table.NewRow 
    row("pic") = GetImageData(<PATH YO YOUR IMAGE FILE>) 
    table.Rows.Add(row) 
    table.AcceptChanges() 

    Dim crpt As New crpt 
    crpt.SetDataSource(table) 

    crv.ReportSource = crpt 
End Sub 






Public Function GetImageData(ByVal cFileName As String) As Byte() 
    Dim fs As System.IO.FileStream = _ 
      New System.IO.FileStream(cFileName, _ 
      System.IO.FileMode.Open, System.IO.FileAccess.Read) 
    Dim br As System.IO.BinaryReader = New System.IO.BinaryReader(fs) 
    Return (br.ReadBytes(Convert.ToInt32(br.BaseStream.Length))) 
End Function 
End Class 

名称的控件如下所示:

按钮= Button1的 的CrystalReportViewer = CRV

表= Form1中 CrystalReport = crpt

TestReport.XSD包含以下内容:

<?xml version="1.0" standalone="yes"?> 
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="test" msdata:UseCurrentLocale="true"> 
<xs:complexType> 
    <xs:choice minOccurs="0" maxOccurs="unbounded"> 
    <xs:element name="test"> 
     <xs:complexType> 
     <xs:sequence> 
      <xs:element name="id" msdata:AutoIncrement="true" type="xs:int" minOccurs="0" /> 
      <xs:element name="pic" type="xs:base64Binary" minOccurs="0" /> 
     </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
    </xs:choice> 
</xs:complexType> 
</xs:element> 
</xs:schema> 
相关问题