c # Crystal Reports Tutorial

c # Crystal Reports Tutorial

http://apps.hi.baidu.com/share/detail/24298108

Crystal Reports is a powerful reporting tool, it has now been Microsoft Visual Studio 2005 (hereinafter referred to VS2005) integrated. Like Crystal Reports friends you can easily use. I put Crystal Reports summarize the use vs2005, for your reference.
I first introduce the software environment used: Microsoft Visual Studio 2005; Microsoft SQL Server 2005
[Example] data
server: SQLEXPRESS
database name: Test
database table: T

[DESCRIPTION]
Crystal Reports Application division two methods, namely a pull mode (the PULL), push mode (PUSH). Pull mode: when the Crystal Reports source data generated is extracted from the Crystal Reports document SQL statement from the database, when programming without rewriting SQL statements, but to add login information (specific methods, described later). Push mode: When the data source generated Crystal Reports, Crystal Reports rewrite the SQL statement generated by the image dataset on programming. In other words, push mode is dataset assembled Crystal Reports.
Crystal Reports component introduction. Crystal Reports in VS2005, there are two components, the project is in the WEB are CrystalReportSource, CrystalReportViewer. FORM is where the project are crystalReport, CrystalReportViewer.
CrystalReportSource, crystalReport Crystal Reports is a data provider; CrystalReportViewer browser Crystal Reports. Also tell us about the water report file is rpt file extension, the file can be generated with VS2005.
The following describes specific methods of operation:
a pull mode (the PULL):
use condition parameter when coupled to a SQL statement such as Crystal Reports in pull mode in {? } Parameter name given embodiment. Example: "Select T1, T2, T3 FROM T Where T1 = '' {? Parm} ''" parm parameter name is

The following examples are used in the SQL statement used Crystal report file is "Select T1, T2, T3 FROM T Where T1 = '' {? Parm} ''" parm is the parameter name.
[Next] WEB way
a using CrystalDecisions.Shared;
a using CrystalDecisions.CrystalReports.Engine;
    /// <the Summary>
    /// function: pull mode extraction Crystal Reports
   /// profile: HTTP: //www.dzend.com/
    / // </ Summary>
    /// <param name = "SENDER"> </ param>
    /// <param name = "E"> </ param>
    protected void Button_pull_Click (SENDER Object, EventArgs E)
{
// CrystalReport crystal reports .rpt is the name of the file; CrystalReportSource1 is added to the toolbox from the data source to crystal reports images on the page.

        CrystalReportSource1.ReportDocument.Load (Server.MapPath ( "CrystalReport.rpt"));
// SetDatabaseLogon pull mode must use this method to set the login information, a parameter: User name; Parameters II: password; three parameters: server; four parameters: the database name
        CrystalReportSource1.ReportDocument.SetDatabaseLogon ( "SA", "123456", @ "SYWZSWL \ SQLEXPRESS", "the Test");
// pass parameters to crystal reports, a parameter: parameter name, parameters II: parameter value;
        CrystalReportSource1.ReportDocument.SetParameterValue ( "the Title", "this is a test report");
        CrystalReportSource1.ReportDocument.SetParameterValue ( "Parm", "1");
// bind crystal reports data sources.
        CrystalReportSource1.DataBind ();
// CrystalReportViewer1 is Crystal Report Viewer, the following is assigned to the browser on the image on
        CrystalReportViewer1.ReportSource = CrystalReportSource1;
        CrystalReportViewer1.DataBind ();

    } Under way FORM]
// Code in the same way as WEB FORM way, with crystalReport controls replaced the CrystalReportSource; crystalReportViewer replaced by the CrystalReportViewer; both controls can be found in the toolbox. Simultaneously remove DataBind () method at the time of programming.
        void the Form1_Load Private (SENDER Object, EventArgs E)
        {

            crystalReport1.Load(Application.StartupPath + "CrystalReport.rpt");

            crystalReport1.SetDatabaseLogon("sa", "123456", @"SYWZSWL\SQLEXPRESS", "Test");

            crystalReport1.SetParameterValue ( "Title", "This is a test report");
            crystalReport1.SetParameterValue ( "Parm", "1");
            crystalReportViewer1.ReportSource = crystalReport1;

        }


Push Mode (PUSH):
Dataset programming assembled in a field in the SQL statement to be consistent with the Crystal Reports in the SQL Statement field in push mode. Simply put, Crystal Reports Push mode is a template, set up after the good form in the designer in the report, and then assembled DataSet can generate a report.


[Under way] WEB

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;
       protected void Button_push_Click(object sender, EventArgs e)
    {
        string sql = "Select T1, T2, T3 FROM T where T1=''a''";
        string DBConfig_sql =@"Data Source=SYWZSWL\SQLEXPRESS;Initial Catalog=Test;User ID=sa;Password=123456";
        DataSet ds = new DataSet();
            SqlConnection sqlCon = new SqlConnection(DBConfig_sql);
            SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
            SqlDataAdapter sqlAd = new SqlDataAdapter();
            sqlAd.SelectCommand = sqlCmd;
            sqlAd.Fill(ds, "sql");
        CrystalReportSource1.ReportDocument.Load (Server.MapPath ( "CrystalReport.rpt"));   
        // note that here the necessary specify the name of the table in the Dataset, otherwise it will prompt "the report you requested more information is needed."
CrystalReportSource1.ReportDocument. SetDataSource (ds.Tables [ "SQL"]);
// can not assign the parameters, even if assigned a value not function {?}.
       CrystalReportSource1.ReportDocument.ParameterFields // [ "Parm"] CurrentValues.AddValue ( "1234567");.
        ( "Report Sample then push mode!") CrystalReportSource1.ReportDocument.ParameterFields [ "Title" ] CurrentValues.AddValue.;
        CrystalReportSource1.DataBind ();

        CrystalReportViewer1.ReportSource = CrystalReportSource1;
         CrystalReportViewer1.DataBind();
    }
【FORM方式下】
private void Form1_Load(object sender, EventArgs e)
        {
            //推模式
            string sql = "Select T1, T2, T3 FROM T where T1=''a''";
            string DBConfig_sql = @"Data Source=SYWZSWL\SQLEXPRESS;Initial Catalog=Test;User ID=sa;Password=123456";
            DataSet ds = new DataSet();
            SqlConnection sqlCon = new SqlConnection(DBConfig_sql);
            SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
            SqlDataAdapter sqlAd = new SqlDataAdapter();
            sqlAd.SelectCommand = sqlCmd;
            sqlAd.Fill (DS, "SQL");
            crystalReport1.Load (Application.StartupPath + "CrystalReport.rpt");
            crystalReport1.SetDataSource (ds.Tables [ "SQL"]);
{?} // parameters can not assignment, even if the value assigned does not work.
            CrystalReportSource1.ReportDocument.ParameterFields // [ "Parm"] CurrentValues.AddValue ( "1234567");.
            ( "Report Sample then push mode!") CrystalReport1.ParameterFields [ "Title" ] CurrentValues.AddValue.;

            crystalReportViewer1.ReportSource = crystalReport1;
}


------------------------------------------------------------------------------------------------------------------------------------------------------------


Crystal Reports program to use

1. how to make the group header, in the same group each page displayed
     when inserted into the group, select the "Repeat group header on each page" in the common options page 
2. How to pass parameters ...... CrystalReportViewer and ReportDocument 
     a , the CrystalReportViewer
             ...... by CrystalReportViewer.ParameterFieldInfo attribute
            1, crystalreportviewer members of the
                  public instance property
                  ParameterFieldInfo (inherited from CrystalReportViewerbase) ParameterFields. Gets or sets the parameter fields collection. 
     Two, reportdocument
             by ReportDocument.DataDefinition.ParameterFields property ......
            1, reportdocument members of the
            public instance property
            DataDefinition DataDefinition. Get DataDefinition object. 
            2, datadefinition members of the
            public instance properties
            ParameterFields ParameterFieldDefinitions. Get ParameterFieldDefinitions collection. 
     Third, run custom parameter field (crystalreportviewer)
             Reference:
            setting parameters | Windows Forms Viewer binding options | Web Forms Viewer binding option 
            supports user input via parameters in Crystal reports. Such parameters have multiple uses. For example: 
            that the parameter based on the database and allows the user to specify a field value of the field, in order to filter the data in the report. 
            Parameter field applied to the report format condition. 
            Parameter field to define the sort order. 
            The following example shows how to set parameters through code field values at runtime. This example explains how to set up two different parameters: the first is a multi-value discrete parameter, the second parameter is the region. 
Modify the parameter field at run time
[c #] 
// statement to pass parameters to
the required variables // viewer control.
= New new ParameterFields paramFields ParameterFields ();
the ParameterField paramField the ParameterField new new = ();
ParameterDiscreteValue discreteVal new new ParameterDiscreteValue = ();
= New new ParameterRangeValue rangeVal ParameterRangeValue ();
// The first parameter is a parameter having a plurality of discrete values.
// Set the name of the parameter field, it must
// parameters and match reports.
paramField.ParameterFieldName = "Customer Name";
// Set the first discrete value and pass it to this parameter.
= discreteVal.Value "the AIC Childrens";
paramField.CurrentValues.Add (discreteVal);
// Sets the second discrete value and pass it to this parameter.
// discreteVal variable is set to a new value, so that the previous settings
// will not be covered.
= new new ParameterDiscreteValue discreteVal ();
discreteVal.Value = "Aruba Sport";
paramField.CurrentValues.Add (discreteVal);
// add parameters to the parameter field set.
; paramFields.Add (paramField)
// second parameter value for the region. paramField variable
// is set to the new value, so that the previous settings will not be covered.
= the ParameterField new new paramField ();
// Set the name of a parameter field, it must
// parameters and match reports.
paramField.ParameterFieldName = "Customer ID";
start and end values // set the range and range to pass
// this parameter.
= 42 is rangeVal.StartValue;
rangeVal.EndValue = 72;
paramField.CurrentValues.Add (rangeVal);
// add a second parameter to the parameter field set.
paramFields.Add (paramField);
// set of parameter fields into the viewer control.
crystalReportViewer1.ParameterFieldInfo = paramFields;
for more information, please see: HTTP: //www.devedu.com/develop/2005-4-8/12305/default.aspx
3. Set Crystal Report Viewer looks
     set Crystal Report Viewer properties:
     BestFitPage Boolean value. Gets or sets the page view is the right size or crop with a scroll bar.
     This place later set to false, set the width of the Crystal Report Viewer you can remove the scroll bar.
     Description: There are two situations in which there will be a problem when printing from the Web Forms viewing device:
     BestFitPage attribute to the default value of "true" (ie, no vertical or horizontal scroll bar), but PageZoomFactor greater than 100. 
     BestFitPage set to "false", the height of the viewer Web Forms pages is less than the height of the report (i.e., with a vertical scroll bar), and the viewer is equal to or greater than the width of the report page width (i.e., no horizontal scroll bar).
     With set Width, Height to achieve showed no gaps and no scroll bars!
     DisplayGroupTree Boolean value. Gets or sets the tree view is visible or hidden. 
    DisplayPage Boolean value. Gets or sets the toolbar is visible or hidden. 
    DisplayToolbar Boolean value. Gets or sets Go to Page button on the toolbar is visible or hidden. 
    PageZoomFactor Int32. Gets or sets the zoom factor of the report. 

    SeparatePages Boolean value. Gets or sets the report pages are separated or connected.
    PageToTreeRatio Float64. Provided the ratio between the size of the report set and the tree view.
4. Control Toolbar buttons:
    set the properties of the Crystal Report Viewer:
    HasGotoPageButton Boolean value. Gets or sets go to page buttons visibility. 
    HasLevelUpButton Boolean value. Gets or sets the Go Back button on the toolbar is visible or hidden. 
    HasPageNavigationButtons Boolean value. Gets or sets the page navigation buttons on the toolbar is visible or hidden. 
    HasRefreshButton Boolean value. Gets or sets the refresh button on the toolbar is visible or hidden. 
    HasSearchButton Boolean value. Gets or sets the search button on the toolbar is visible or hidden. 
    HasZoomFactorList Boolean value. Gets or sets the zoom factor list on the toolbar is visible or hidden.
The layout of the Web pages:
    In the "Design View", modify the CrystalReportViewer Width, Height property.
    Switch to form "the HTML View" style attribute modification.
<CR: CrystalReportViewer id = "CrystalReportViewer1 " style = "Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat = "server" Width = "350px" Height = "50px" EnableDrillDown = "False" = DisplayGroupTree "False"> </ CR: the CrystalReportViewer>
6. the other company crystal reports LOGO
    will replace or remove the crystal ...... company Logo file
    (1) if it is used in carrying crystal reports VS.NET
C: \ Files Program \ in the Microsoft Visual Studio the .NET \ the crystal reports \ Viewers \ ImagesRF Royalty Free \ ToolBar \ logo.gif
    (2) If you are using crystal reports 9.2
C: \ Program Files \ the Common Files \ the crystal Decisions \ 2.0 \ crystalreportviewers \ ImagesRF Royalty Free \ Toolbar \ crlogo.

    (1) If you are using VS.NET comes with Crystal Reports in
C: \ Program Files \ in the Microsoft Visual Studio the .NET \ the Crystal Reports \ Viewers \ ImagesRF Royalty Free
    (2) If you are using Crystal Reports 9.2
C: \ Program Files \ the Common Files \ the Crystal Decisions \ 2.0 \ crystalreportviewers \ ImagesRF Royalty Free
8. when printing on installed plug-ins:
    in the updated version of Crystal Reports, you can use the new CrystalReportViewer.PrintMode attribute to specify the print mode. Which contains two options: ActiveX and PDF. 
   When the property is set to PrintMode.PDF, the report will be exported on the WEB server as a PDF, and then the data stream to the browser, users can use the option to print directly to the printer. This option is cross-platform compatible.
When the property is set to PrintMode.ActiveX, ActiveX controls allow the user to print a report to print directly to a local printer

to download http://support.businessobjects.com/CRforVS2005/PrintControl.cab, put on their own servers.

To display cab package in Crystal Reports for Visual Studio 2005 Web site, the need to join the web.config file the following statement xml website (must be added to the <configuration xmlns = "http://schemas.microsoft.com/.NetConfiguration/v2. 0 "> later, in the front):
<configSections>
<sectionGroup name =" businessObjects ">
   <sectionGroup name =" CrystalReports ">
    <Section name =" PrintControl "of the type =" System.Configuration.NameValueSectionHandler, System, Version = 1.0.3300.0, Culture = Neutral, the PublicKeyToken = b77a5c561934e089, the Custom = null "/>
   </ sectionGroup>
</ sectionGroup>
</ configSections>
<businessObjects>
<CrystalReports>
   <PrintControl>
    <the Add Key =" URL "value =" HTTP : //192.168.88.91 / PrintControl.cab "/> - the address where the package is located to .CAB
   </ PrintControl>
</ CrystalReports>
</ businessObjects>
Note: only Internet Explorer will support ActiveX mode. When printing from a non-Internet Explorer browsers (FireFox, Safari, Mozilla, etc.), will be restored to the PDF pop-up dialog box.

Then the printed page to add a layer or directly in the main form, and add the following code in the layer (other means may be employed)
<Object ID = "CrystalPrintControl" ClassID = "the CLSID: BAEE131D-290A-4541-A50A-8936F159563A"
    = the codebase "http://192.168.88.91/printcontrol.cab" height = "0px" version = "10,2,0,1078"
    VIEWASTEXT = "" width = "0px">
</ Object>
version is the version number, If you are other versions of the version number to change it.
9. The process has reached the maximum number of report processing jobs limit the system administrator to configure the
   solution:
   specifically modify the following two key values.
   HKEY_LOCAL_MACHINE; SOFTWARE; the Crystal Decisions; 10.0; the Report the Application
   Server; InprocServer32; PrintJobLimit modified to 1000
   and a HKEY_LOCAL_MACHINE; SOFTWARE;
   Server; Server; PrintJobLimit also revised to 1000 
   I found in C: \ WINDOWS \ Temp that there are a lot of temporary files Crystal Reports files will be generated once every several files, in case the computer does not restart, it will not is deleted, and a lot of useless files appear in google search inside a circle found it was faced with this very vague answer, but just say crystal reports document you want to close off loaded from here we can see such a human error should be as into the programmer.
    specific solutions are as follows:
          1.ReportDocumen instance must be a member of the class    
             Private PrTP the ReportDocument the ReportDocument new new = ();   
          2. End crystal reports file must be closed in this way the windows will not be temporary file generated inside.
              Private void the Page_Unload (SENDER Object, EventArgs E)
              {
                    prtp.Dispose ();
              }
             the Page_Unload event is running when a page is completely displayed, solved this way.

Guess you like

Origin www.cnblogs.com/itchenguo/p/11026446.html