Use Crystal Report in Visual Studio .NET (below)

zhaozj2021-02-17  48

Use Crystal Report in Visual Studio .NET (below)

From www.aspfree.comTranslated by Cash (Tianxia Seventh) Cashcao@msn.com

Crystal Report Demo - Using Push Model

Let's take a look at how to use the Push Model to implement Crystal Reports

1. Create a DataSet when design

2. Create a .rpt file and point to the DataSet you created in front.

3. Place the Crystal Report Viewer control on the .aspx page, set its properties to the.rpt file that is created step by step.

4. In the Code Behind page, write the function of the connection database

5. Plus the DataBind method.

Create a DataSet when designing a DataSet to define the Fielsds for Reports.

1) Right click on "Solution Explorer" and select "Add" -> SELECT "Add new item -> SELECT" dataset "

2) Drag into the "Stores" table from "SQL Server" in the "Server Explorer" panel

3) This will create a "Stores" table in DataSet.

The .xsd file created with this method contains only the definition of the Field, there is no data. You need to create a link to the database and pop it in.

Create a .rpt file

4) Create a .rpt file. The only difference in front is whether you get a table with Crystal Report, we will use DataSet to create it.

5) After establishing the .rpt file, right-click "Details" section, select "Add / Remove Database"

6) In the "Database Expert" window, expand "Project Data", expand "ADO.NET DATASET", "Dataset1", select "Stores" Table.

7) Click ">" to include "STORES" Table includes "SELECTED TABLES"

8) Next, set the layout of the Report.

Create a Crystal Report Viewer Control

9) The next step is to create a Crystal Report Viewer Control with PULL MODEL and set its properties.

Change Code Behind Page Code:

10) Designed for your page load as follows:

Sub bindreport ()

Dim myconnection as new sqlclient.sqlConnection ()

MyConnection.connectionstring = "server = (local) / netsdk; database = pubs; trusted_connection = yes"

Dim MyCommand as new sqlclient.sqlcommand ()

Mycommand.connection = myconnection

Mycommand.commandtext = "SELECT * from Store"

Mycommand.commandtype = commandtype.text

Dim myda as new sqlclient.sqldataadapter ()

Myda.selectcommand = myCommand

DIM MYDS As New DataSet1 () 'this is Our DataSet Created At Design Time

Myda.Fill (MyDS, "Stores")

'You Have to Use the Same Name As That of Your DataSet That You create DURING DESIGN TIME

Dim ORPT AS New CrystalReport1 ()

'This is The Crystal Report File Created At Design Time

ORPT.SetDataSource (MYDS)

'Set the setDataSource Property of the report to the dataset

CrystalReportViewer1.Reportsource = ORPT

'Set The Crystal Report Viewer's Property To The ORPT Report Object That We create

End Sub

Note: In the code above, you may notice the ORPT object is an instance of "Strongly Typed" Report File. If we use "untyped" Report, we will have to use the ReportDocument object and manually load this report file.

Run your program

11) F5 runs.

Output Report files to another format

You can choose to output your report file into the following format:

PDF (Portable Document Format)

2. DOC (MS Word Document)

3. XLS (MS Excel Spreadsheet)

4. HTML (Hyper Text Markup Language - 3.2 Or 4.0 Compliant)

5. RTF (Rich Text Format)

In fact, you can place a button to raise an output function.

Output a Report file created with PULL MODEL

When outputting a Report file created with PULL MODEL, Crystal Report is very sensitive to the connection to the database and the required record, so you can only use the code provided below:

Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click

Dim myreport as crystalreport1 = new crystalReport1 ()

'NOTE: WE Are Creating An Instance of The Strongly-Typed Crystal Report File Here.

DIM DISKOPTS as crystaldecisions.shared.diskfileDestinationOptions = new crystaldecisions.shared.diskFileDESTINATIONOPTIONOS ()

MyReport.ExportOptions.exportDestinationType = crystaldecisions. [Shared] .exportDestinationType.Diskfile

'You also harve the option to export the report to other sources

'Like Microsoft Exchange, Mapi, etc.

MyReport.ExportOptions.exportFormattype = crystaldecisions. [shared] .exportformattype.portabledocformat'here We are exporting the report to a .pdf format. you can

'Also choose any of the other.

Diskopts.diskFileName = "c: /output.pdf"

'If you do not specify the exact path here (i.e. incruding

'The Drive and Directory,

'Ten you Would Find your Output File Landing Up in The

'C: / Winnt / System32 Directory - Atleast In Case of A

'Windows 2000 System

MyReport.exportOptions.DestinationOptions = Diskopts

'The Reports Export Options Does Not Have A FileName Property

'That Can Be Directly Set. INSTEAD, You Will Have To Use

'The DiskFileDESTINATIONOPTIONOPFILENAME

'Property to The File Name (Including The Path) of Your Choice.

'Then You Would Set The Report Export Options

'DestinationOptions property to point to the

'DiskFileDESTINATIONOPTION Object.

myreport.export ()

'This Statement Exports The Report Based on The Previously Set Properties.

End Sub

Output a Report file created by PUSH MODEL

When an REPORT file created by the PUSH Model is output, the first step is to manually create a connection and bind the database. Set this Reports 'setDataSource' for this DataSet (as described above). Then you can call the code shown above.

____________________________________________________________

Submit:

Ajay Varghese & Shankar N.s.

Sr. Software Engineers,

Jarvis Infotech

转载请注明原文地址:https://www.9cbs.com/read-28486.html

New Post(0)