How to use Crystal Reports without a tightly-linke

2019-01-26 17:04发布

问题:

I'm learning to use Crystal Reports (with VB 2005).

Most of what I've seen so far involves slurping data directly from a database, which is fine if that's all you want to display in the report.

My DB has a lot of foreign keys, so the way I've tried to stay sane with presenting actual information in my app is to add extra members to my objects that contain strings (descriptions) of what the foreign keys represent. Like:

Class AssetIdentifier

    Private ID_AssetIdentifier As Integer
    Private AssetID As Integer
    Private IdentifierTypeID As Integer
    Private IdentifierType As String
    Private IdentifierText As String

    ...

Here, IdentifierTypeID is a foreign key, and I look up the value in a different table and place it in IdentifierType. That way I have the text description right in the object and I can carry it around with the other stuff.

So, on to my Crystal Reports question.

Crystal Reports seems to make it straightforward to hook up to records in a particular table (especially with the Experts), but that's all you get.

Ideally, I'd like to make a list of my classes, like

Dim assetIdentifiers as New List(Of AssetIdentifier)

and pass that to a Crystal Report instead of doing a tight link to a particular DB, have most of the work done for me but leaving me to work around the part that it doesn't do. The closest I can see so far is an ADO.NET dataset, but even that seems far removed. I'm already handling queries myself fine: I have all kinds of functions that return List(Of Whatever) based on queries.

Is there an easy way to do this?

Thanks in advance!

UPDATE: OK, I found something here:

http://msdn.microsoft.com/en-us/library/ms227595(VS.80).aspx

but it only appears to give this capability for web projects or web applications. Am I out of luck if I want to integrate into a standalone application?

回答1:

Go ahead and create the stock object as described in the link you posted and create the report (StockObjectsReport) as they specify. In this simplified example I simply add a report viewer (crystalReportViewer1) to a form (Form1) and then use the following code in the Form_Load event.

stock s1 = new stock("AWRK", 1200, 28.47);
stock s2 = new stock("CTSO", 800, 128.69);
stock s3 = new stock("LTWR", 1800, 12.95);

ArrayList stockValues = new ArrayList();

stockValues.Add(s1);
stockValues.Add(s2);
stockValues.Add(s3);

ReportDocument StockObjectsReport = new StockObjectsReport();
StockObjectsReport.SetDataSource(stockValues);

crystalReportViewer1.ReportSource = StockObjectsReport;

This should populate your report with the 3 values from the stock object in a Windows Form.

EDIT: Sorry, I just realized that your question was in VB, but my example is in C#. You should get the general idea. :)



回答2:

I'm loading the report by filename and it is working perfect:

//........

ReportDocument StockObjectsReport;

string reportPath = Server.MapPath("StockObjectsReport.rpt");

StockObjectsReport.Load(reportPath);

StockObjectsReport.SetDataSource(stockValues);

//Export PDF To Disk

string filePath = Server.MapPath("StockObjectsReport.pdf");

StockObjectsReport.ExportToDisk(ExportFormatType.PortableDocFormat, filePath);