1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

C# Crystal Reports from SQL Query

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.18 MB, 71 trang )


the following sql and generating the report

sql = "SELECT Product_id,Product_name,Product_price FROM Product";

Create a new C# project and create a new Strongly Typed Dataset from

Project - Add New Item Dialogue Box.

Add three column in the Strongly Typed Dataset .

Product_id

Product_name

Product_price



Create a new Crystal Report and select DataTable as Data Source . You can

select DataTable from the wizard , Project Data - ADO.NET Dataset Crystal report Dataset1 - dataset1. Click the Next Button.



Select fields ( Product_id , Product_name , Product_price ) from the next



screen and click the finish button. Then you will get the designer screen with

the selected fields. If you do not know how to create a dataset , refer the

previous section How to create a C# Crystal Report from Strongly Typed

Dataset.

Now the designing part is over. From the source code we can pass the SQL

source code to Crystal Reports.

Select the default form (Form1.cs) you created in C# and drag a button and a

CrystalReportViewer control to your form .

You have to include CrystalDecisions.CrystalReports.Engine in your C#

Source Code.

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

Copy and paste the following source code and run your C# project

using

using

using

using

using

using



System;

System.Windows.Forms;

CrystalDecisions.CrystalReports.Engine;

CrystalDecisions.Shared;

System.Data;

System.Data.SqlClient ;



namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

SqlConnection cnn ;

string connectionString = null;

string sql = null;

connectionString = "data source=SERVERNAME;initial

catalog=DATABASENAME;user id=USERNAME;password=PASSWORD;";

cnn = new SqlConnection(connectionString);

cnn.Open();

sql = "SELECT Product_id,Product_name,Product_price FROM



Product";



}

}



SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);

DataSet1 ds = new DataSet1();

dscmd.Fill(ds, "Product");

MessageBox.Show (ds.Tables[1].Rows.Count.ToString());

cnn.Close();

CrystalReport1 objRpt = new CrystalReport1();

objRpt.SetDataSource(ds.Tables[1]);

crystalReportViewer1.ReportSource = objRpt;

crystalReportViewer1.Refresh();



}



connectionString = "data source=SERVERNAME;initial

catalog=DATABASENAME;user

id=USERNAME;password=PASSWORD;";

You have to provide the necessary database information to

Connection String.

15. C# Crystal Reports from XML



Usually we are generating Crystal Reports from Databases , here in the

following section describes how to create a Crystal reports from XML file in

C#. This is very similar to creating Crystal Reports from database , the only

difference is that you have to select the data source as the XML file at the

designing time of Crystal Report in C#.

All C# Crystal Reports Tutorial in this website is based on the following

database - crystaldb. So before you begin this section , please take a look at the

database structure of crystaldb - Click Here C# crystaldb

If you are new to Crystal Reports and do not know how to create Crystal

Reports from C# , please take a look at the section step by step tutorial for

creating a Crystal Reports from C#.

In order to generating Crystal Report from XML file , we have to create an

XML file . Here we are going to create an XML file and name it as

Product.XML . The structure of the Product.XML file is same as the Product



table in the crystaldb database earlier mentioned in this section C# crystaldb .

The content of the product.xml is shown below.



Download Product.XML

Generating Crystal Report from XML file is very similar to generating Crystal

Report from Databases. The only difference is happened when you selecting

the datasource part. Here you have to select Create New Connection Database Files and select the XML file you want to generate Crystal Reports

(In this case you select the Product.xml ).



Select all the fields from Product and click finish button

Now the designing part is over and the next step is to call the Crystal Reports in

C# and view it in Crystal Reports Viewer control .

Select the default form (Form1.cs) you created in C# and drag a button and a

CrystalReportViewer control to your form .

You have to include CrystalDecisions.CrystalReports.Engine in your C#

Source Code.

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

Copy and paste the following source code and run your C# project

using

using

using

using



System;

System.Windows.Forms;

CrystalDecisions.CrystalReports.Engine;

CrystalDecisions.Shared;



namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();



}

private void button1_Click(object sender, EventArgs e)

{

ReportDocument cryRpt = new ReportDocument();

cryRpt.Load(PUT CRYSTAL REPORT PATH

HERE\\CrystalReport1.rpt");

crystalReportViewer1.ReportSource = cryRpt;

crystalReportViewer1.Refresh();

}

}

}



NOTES:

cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");

The Crystal Reports file path in your C# project files location, there you can

see CrystalReport1.rpt . So give the full path name of Crystal Reports file like

c:\projects\crystalreports\CrystalReport1.rpt

16. C# Crystal Reports - sub reports

The following progrm describes how to create a sub report within a Crystal

Report in C#.

All C# Crystal Reports Tutorial in this website is based on the following

database - crystaldb. So before you begin this section , please take a look at the

database structure of crystaldb - Click Here C# crystaldb

If you are new to Crystal Reports and do not know how to create Crystal

Reports from C# , please take a look at the section step by step tutorial for

creating a Crystal Reports from C#.

Subreport in Crystal Reports means that a report inside a Crystal Report . When

we want to show some additional information about a data field in a Crystal

Reports row, we use the subreports features to show the details. We can display

sub report in two ways , one is to show subreport directly under the main row

details. The other method is to show it as C# Crystal Reports on demand subreports , that means we put an Hyper link just below the row filed , and when

the user click that hyper link it will show the subreport.



In this section we are going to generate a sub-report under the main row details.

That is for each row there is a subreoprt for the details.



Here we are going create an order report based on three tables in the database

and show a subreoprt for each row field of Product Name. Here we are using

our earlier program C# Crystal Reports from multiple tables to show the main

Crystal Reports Data , so refer C# Crystal Reports from multiple tables , before

start this section .

Create a Crystal Reports using three tables and select customername , date ,

product and qty . It will explain in detail the previous section C# Crystal

Reports from multiple tables.

Next step is to create a sub report inside the main report. Here we are showing

the Product details in each row of the specified product in the main row.

After create the main report , right click on Crystal Reports designer window

and select Insert-Subreport.



Xem Thêm
Tải bản đầy đủ (.pdf) (71 trang)

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×