The use of the report, divided into two ways: pull and push
Pull and push models
in order to provide the most flexible data access methods to the developer, Crystal Reports database driver is designed to pull model and a push model can provide data access at the same time.
Pull model
Under the pull model, the driver connects to the database and the data needed to "pull" Come. When using this model, and the connection to the database for SQL commands and data acquisition performed simultaneously processed by Crystal Reports itself, requiring the developer to write code. If you do not need to write any special code at runtime, use the pull model.
Push model
Instead, the push model requires the developer to write code to connect to the database, to create and execute SQL commands matching fields in the report record set or data sets, and to pass the object to the report. This method allows you to connect into shared applications, and the data is first filtered out before the data in Crystal Reports received.
Which recommend the use of push, pull because of its relatively good performance, specific can go online to view the content description.
Here, I just push describe; (personal order of operations, but can only show a method, there should be other ways)
First, create a data set, if it is (DataSet1.xsd), provided after a good set of data, and then creates crystralreport1.rpt, crystralreport.rpt data connection set up, and after the report design is good, then we must create a web form (e.g., a Crystral_Use.aspx), wherein the page to be dragged into a CrystalReportViewer control, followed by the following operations:
Secondly, corresponding references namespace using CrystalDecisions.Shared; and using CrystalDecisions.CrystalReports.Engine;
Finally, the following programming crystral-use.aspx page:
CrystalReport1 crvs = new CrystalReport1 (); // instantiate report
private void Page_Load(object sender, System.EventArgs e)
{
// here Put user code to initialize the page
string connstring = "server = .; database = BMSShopping; uid = sa; pwd = 123456;"; // database connection SqlConnection conn = new SqlConnection (connstring);
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=new SqlCommand();
da.SelectCommand.CommandText = "select top 3 * from orders"; // da.SelectCommand.Connection = conn retrieving data from the database;
conn.Open();
DataSet1 ds = new DataSet1 (); // custom applications a data set
da.Fill(ds,"Orders");
crvs.SetDataSource(ds);
this.CrystalReportViewer1.ReportSource=crvs;
}
In this way, a simple crystralreport finished on the application;
Extended about:
If you want to export a report, then you continue with the operation settings:
First of all, or to the basis of the above, drag and drop a dropdownlist control in order to select the export format:
Then, write the code:
// Export Report
CrystalDecisions.Shared.DiskFileDestinationOptions DiskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
crvs.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
switch (ddlFormat.SelectedItem.Text)
{
case "RTF format":
crvs.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;//
DiskOpts.DiskFileName = "d: \\ Output.rtf"; // export path
this.Response.Write ( "Export RTF format success!");
break;
case "PDF format":
crvs.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;//
DiskOpts.DiskFileName = "d: \\ Output.pdf"; // export path
this.Response.Write ( "Export PDF format success!");
break;
case "DOC format":
crvs.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;//
DiskOpts.DiskFileName = "d: \\ Output.doc"; // export path
this.Response.Write ( "DOC format export success!");
break;
case "EXCEL format":
crvs.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;//
DiskOpts.DiskFileName = "d: \\ Output.xls"; // export path
this.Response.Write ( "EXCEL format Export success!");
break;
default:
break;
}
crvs.ExportOptions.DestinationOptions = DiskOpts;
crvs.Export();
}
So far, all the relevant report printing to achieve would be finished, maybe there are better methods of operation or, I hope you let me know. As for the visual studio 2005 Crystal Reports operations, compared to visual studio 2003 is much simpler, drag and drop can be achieved. And visual studio 2005 is relatively simple, but beautiful also upgraded.
----------------
Disclaimer: This article is the original article CSDN bloggers "yingzhaom", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/yingzhaom/article/details/7775491