Crystal Report is a powerful reporting tool that has been integrated by Microsoft Visual Studio 2005 (hereinafter referred to as vs2005. The usage of the Crystal Report in vs2005 is as follows:
Software environment: Microsoft Visual Studio 2005; Microsoft SQL Server 2005
[Data use case]
Server: sqlexpress
Database Name: Test
Database Table: T
Solution to the user password input box displayed in the Crystal Report
Tablelogoninfo info = new tablelogoninfo ();
Tablelogoninfos Infos = new tablelogoninfos (); // Note: there is an "S" more than the previous one"
Connectioninfo conn = new connectioninfo ();
Conn. servername = "localhost ";
Conn. datasource = "northwind ";
Conn. userid = "sa ";
Conn. Password = "";
Info. connectioninfo = conn;
Info. tablename = "MERs ";
Infos. Add (Info );
This. crystalreportviewer1.logoninfo = Infos;
This. crystalreportviewer1.reportsource = RPT;
This. crystalreportviewer1.databind ();
Conn. servername = c: \ Documents ents and Settings \ reeezak \ Desktop \ db1.mdb
Conn. databasename = c: \ Documents ents and Settings \ reeezak \ Desktop \ db1.mdb
Conn. Password = 1
Conn. userid = Admin
[Description]
There are two methods for applying a crystal Report: Pull Mode and push mode ). Pull Mode: When the crystal report is generated, the data source is extracted from the SQL statement in the crystal report file from the database. You do not need to rewrite the SQL statement during programming, but you need to add the login information (specific method, later ). Push mode: the data source generated when the crystal report is generated. It is a DataSet object generated by rewriting the SQL statement in the Crystal Report during programming. That is to say, the push mode is to use dataset to assemble the crystal report.
Introduction to the Crystal Report component: There are two types of Components in vs2005: crystalreportsource and crystalreportviewer. In the form project, they are respectively crystalreport and crystalreportviewer.
Crystalreportsource, crystalreport is the data provider of the Crystal Report, and crystalreportviewer is the browser of the crystal report. In addition, we will introduce that the water report file is a file with the RPT extension, which can be generated using vs2005.
The following describes the specific operation methods:
Pull Mode (pull ):
To add a condition parameter to an SQL statement in the Crystal Report in PULL mode, use {? Parameter Name. Example: "select T1, T2, T3 from t where t1 = "{? Parm} "parm is the parameter name
In the following example, the SQL statements USED IN THE CRYSTAL REPORT file are "select T1, T2, T3 from t where t1 = "{? Parm} "parm is the parameter name.
[Web mode]
Using crystaldecisions. shared;
Using crystaldecisions. crystalreports. engine;
///
/// Function: extract the Crystal Report in PULL mode
// Personal homepage: http://www.dzend.com/
///
///
///
Protected void button_pull_click (Object sender, eventargs E)
{
// Crystalreport. rpt is the name of the crystal report file. crystalreportsource1 is the object of the crystal report data source added to the page from the toolbox.
Crystalreportsource1.reportdocument. Load (server. mappath ("crystalreport. rpt "));
// This method must be used in setdatabaselogon PULL mode to set logon information. Parameters 1: User Name; parameter 2: password; parameter 3: Server; parameter 4: Database Name
Crystalreportsource1.reportdocument. setdatabaselogon ("sa", "123456", @ "sywzswl \ sqlexpress", "test ");
// Transmit parameters to the crystal report. Parameter 1: Parameter Name, parameter 2: parameter value;
Crystalreportsource1.reportdocument. setparametervalue ("title", "this is a test report ");
Crystalreportsource1.reportdocument. setparametervalue ("parm", "1 ″);
// Bind the crystal report data source.
Crystalreportsource1.databind ();
// Crystalreportviewer1 is the crystal report browser.
Crystalreportviewer1.reportsource = crystalreportsource1;
Crystalreportviewer1.databind ();
}
[Form mode]
// In form ModeCodeIn the same Web mode, the crystalreportsource is replaced with the crystalreport control, and the crystalreportviewer is replaced with the crystalreportviewer. Both controls can be found in the toolbox. Remove the databind () method during programming.
Private void form1_load (Object sender, 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 ):
In the push mode, the SQL statement fields in the dataset that are programmed and assembled must be consistent with the SQL statement fields in the crystal report. Simply put, the Crystal Report in the push mode is a template. After setting the report format in the designer, assemble dataset to generate the report.
[Web mode]
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 =" "";
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 sqglad = new sqldataadapter ();
Sqglad. selectcommand = sqlcmd;
Sqglad. Fill (DS, "SQL ");
Crystalreportsource1.reportdocument. Load (server. mappath ("crystalreport. rpt "));
// Note that the name of the table in dataset must be specified here. Otherwise, the system prompts "the report you requested requires more information ."
Crystalreportsource1.reportdocument. setdatasource (Ds. Tables ["SQL"]);
//{?} You do not need to assign values to parameters in, even if a value is assigned.
// Crystalreportsource1.reportdocument. parameterfields ["parm"]. currentvalues. addvalue ("1234567 ″);
Crystalreportsource1.reportdocument. parameterfields ["title"]. currentvalues. addvalue ("report sample of Push mode !");
Crystalreportsource1.databind ();
crystalreportviewer1.reportsource = crystalreportsource1;
crystalreportviewer1.databind ();
}< br> [Form mode]
private void form1_load (Object sender, eventargs E)
{< br> // push mode
string SQL = "select T1, T2, T3 from t where t1 =" "";
string dbconfig_ SQL = @ "Data Source = sywzswl \ sqlexpress; initial catalog = test; user id = sa; Password = 123456 ″;
dataset DS = new dataset ();
sqlconnection sqlc On = new sqlconnection (dbconfig_ SQL);
sqlcommand sqlcmd = new sqlcommand (SQL, sqlcon);
sqldataadapter sqglad = new sqldataadapter ();
sqglad. selectcommand = sqlcmd;
sqglad. fill (DS, "SQL");
crystalreport1.load (application. startuppath + "crystalreport. RPT ");
crystalreport1.setdatasource (Ds. tables ["SQL"]);
//{?} You do not need to assign values to parameters in, even if a value is assigned.
// crystalreportsource1.reportdocument. parameterfields ["parm"]. currentvalues. addvalue ("1234567");
crystalreport1.parameterfields ["title"]. currentvalues. addvalue ("this is a report sample in Push mode! ");
Crystalreportviewer1.reportsource = crystalreport1;
}
Http://hi.baidu.com/feiji123/blog/item/b9e807f5008e8c2dbd31095e.html
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/rockywu/archive/2009/08/06/4419928.aspx