I just learned the Crystal Report, which is mainly used in the web of C #. I have made an example on the Internet for your reference.
Create a project first-> Web application, and then
(1) Open the toolbox and drag a crystalreportviewer to the form. we name it rptvew.
(2) Adjust the Windows form viewer to the desired size and move it to the desired location through drag-and-drop operations.
(3) When running an application, the report is displayed in the viewer.
2. Create a new report
(1) point to "add" and click "Add new item ".
(2) In the "Add new project" dialog box, select Crystal Report from the "template" area, name the report rptclient, and click "open ".
(3) in the crystal report library, select one of the following options:
A. Use report experts to guide you through the report creation process and add your selection to the Crystal Report designer.
B. Open the Crystal Report designer as a blank report.
C. From the existing report-> Create a new report, which is the same as the design of another report.
Note that the crystal report library contains many experts who can guide you through the creation of several specific types of reports. You may want to use experts to create the initial report to determine which report construction method is suitable for your needs.
(4) Click OK.
If you select "report expert", the "Report expert" dialog box appears with the data resource manager. Select the required data for each folder, complete the operations on the "Report experts" tab, and click "finish" to access the Crystal Report designer and your reports.
(5) Do I Need To dynamically set the data source?
Crystal Reports connects to the database through the database driver. Each driver is programmed to process a specific database type or database access technology. To provide developers with the most flexible data access methods, the Crystal Reports database driver is designed as a pull model and a push model that provides both data access.
Pull model pull: In the PULL model, the driver will connect to the database and "pull" the data as needed. When using this model, the connection to the database and the SQL commands executed to obtain data are both processed by Crystal Reports, and developers do not need to write code. If you do not need to write any special code at runtime, use the PULL model.
Push model push: on the contrary, the push model requires developers to write code to connect to the database, execute SQL commands to create a record set or dataset that matches the fields in the report, and pass the object to the report. This method allows you to place the connection share in the application and filter the data before Crystal Reports receives the data.
3. Create a report from the ADO. Net Dataset
Create a DataSet object from a database
(1) create a new architecture file in the project:
In Solution Explorer, right-click the project name, point to "add", and then click "Add new project ". In the "category" Area of the "Add new project" dialog box, expand the folder and select "data ". Select "dataset" in the "template" area ". Accept the default name dataset1.xsd. This creates a new schema file (dataset1.xsd), which will be used to generate a strong dataset. The schema file is displayed in the ADO. Net Dataset Designer.
(2) Specify the database location:
In server resource manager, right-click "Data Connection" and select "add connection ". In the "Data Link Properties" dialog box, click the "providers" tab and select a provider (for example, Microsoft ole db provider for SQL Server ). Click the connection tab and specify the location of your database. Enter the server and logon information at the required location. Click OK. In this case, your database, its tables, and fields appear under the "Data Connection" node of the server resource manager.
(3) In Solution Explorer, double-click dataset1.xsd (if it is not an active view yet ). Dataset1.xsd should now be displayed on the dataset tab.
(4) To create a schema for a dataset, drag the required table from the server resource manager to the dataset tab of dataset1.xsd.
(5) Click "Save dataset1.xsd" to save the "dataset1.xsd" file.
(6) On the "generate" menu, click "generate" to generate a DataSet object for the project.
The ADO. Net DataSet object provides a description of the data from which tables can be added to the Crystal Report. Use "database experts" in Crystal Report designer to add tables from ADO. Net dataset objects. Call "Database Expert" when using "report expert" to create a new report ". Or, you must use ADO. to access "database experts" in a report created by. net, right-click report designer, point to "Database", and click "Add/delete database ". Connect a report to an ADO. Net DataSet object
(1) database field-> Add/delete a database. In "database experts", expand the "project data" folder.
(2) Expand the "ADO. Net dataset" folder.
(3) Select the expected DataSet object.
For example, if you are using a DataSet object generated from the project "dataset1.xsd" architecture file "windowsapplication1", you should select "windowsapplication1.dataset1 ". Select the table to be added to the report.
Add code in webform1.aspx. CS
Add
Using system. Data. sqlclient;
Using system. IO;
Using crystaldecisions. crystalreports. engine;
Rptclient orpt = new rptclient (); // preferably written in
// Rptclient orpt1 = new rptclient ();
Private void page_load (Object sender, system. eventargs E)
{
// Load the report
String strprovider = "Server = 192.168.0.1; database = staroa_db; uid = sa; Pwd = l71110101"; sqlconnection myconn = new sqlconnection (strprovider );
Myconn. open ();
String strsel = "select * From star_empdetailinfo ";
Sqldataadapter myadapter = new sqldataadapter (strsel, myconn );
Dataset1 DS = new dataset1 ();
Myadapter. Fill (DS, "star_empdetailinfo ");
// The second table
String strsel1 = "select * From jour_work ";
Sqldataadapter myadapter1 = new sqldataadapter (strsel1, myconn );
Myadapter1.fill (DS, "jour_work ");
Orpt. setdatasource (DS );
Rptvew. reportsource = orpt;
}
Print report
Private void button#click (Object sender, system. eventargs E)
{
// Specify the printer name
String strprintername;
Strprintername = @ "HP LaserJet 3380 PCL 6"; // you can change the printer name to use it, but I don't think it is good, because it won't work if you change a printer, the printed question will be studied again next time.
// Set the print margin
Pagemargins margins;
// Margins = orpt. printoptions. pagemargins;
Margins = orpt. printoptions. pagemargins;
Margins. bottommargin = 250;
Margins. leftmargin = 350;
Margins. rightmargin = 350;
Margins. topmargin = 450;
Orpt. printoptions. applypagemargins (margins );
// Application printer name
Orpt. printoptions. printername = strprintername;
// Print the report. Set startpagen and endpagen
// If the parameter is set to 0, all pages are printed.
Orpt. printtoprinter (1, false, 0, 0 );
}
A simple report that connects two tables is complete.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1011218