c#水晶报表连接mysql_[C#][双语]水晶报表入门3-第一个报表

本文为C#新手提供了一步一步创建水晶报表的教程。使用Visual Studio创建Windows项目,添加Crystal Reports,选择OLE DB连接到SQL Server上的crystaldb数据库,展示Product表中的所有数据。在Crystal Reports设计窗口中排列字段,然后在C#应用中调用报表并用CrystalReportViewer控件查看。
摘要由CSDN通过智能技术生成

译文仅供本博主参考自用

A step by step tutorial for beginners who is creating their

Crystal Reports for the first time in C#

跟着教程一步步做自己的第一个水晶报表。

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

following database – crytaldb. So before you begin this section,

please take a look at the database structure of crystaldb – Click

Here C#

crystaldb

所以水晶报表的例子都是基于crystaldb这个数据库的。你可以点击这里查看一下crystaldb的结构。

Here we are going to create a new Crystal Reports in C# from

Product table in the above mentioned database crystaldb. The

Product table has three fields(Product_id, Product_name,

Product_price) and we are showing the whole data from Product table

to the C#-Crystal Reports project.

我们将新建一个水晶报表用来显示Product表里的数据。Product表有三个字段Product_id,

Product_name, Product_price。我们将显示Product中的所有数据。

Open Visual Studio .NET and select a new CSharp Windows

project.

打开VS,选择新建一个Windows Application项目

a4c26d1e5885305701be709a3d33442f.png

Now you will get the default Form1.cs.

你会看到一个缺省的文件Form1.cs

From the main menu in Visual Studio C# project select

project->Add New Item. Then Add New Item dialogue

will appear and select Crystal Reports from the dialogue box.

在VS编辑器上面的菜单中选择Project,再选Add New Item, 就会弹出一个对话框,在这里选择Crystal

Report.

a4c26d1e5885305701be709a3d33442f.png

Select Report type from Crystal Reports gallery.

选择报表类型

a4c26d1e5885305701be709a3d33442f.png

Accept the default settings and click OK.

缺省设置,选择OK

Next step is to select the appropriate connection to your

database (here crystaldb). Here we are going to select OLEDB

Connection for SQL Server to connect Crystal Reports in C#.

下一步就是选择合适的连接方式(这里我们要连接crystaldb)我们选择OLEDB Connection for SQL

Server来连接crystaldb

Select OLE DB(ADO) from Create New Connection.

在下图中选择OLE DB(ADO)

a4c26d1e5885305701be709a3d33442f.png

Select Microsoft OLE DB Provider for SQL Server

选择Microsoft OLE DB Provider for SQL Server

a4c26d1e5885305701be709a3d33442f.png

The next screen is the SQL Server authentication screen for

connecting to the database-crystaldb. Select your Sql Server name,

enter userid, password and select your Database Name.

下图是SQL Server链接服务器和数据库的认证界面。选择SQL Server的名称和对应数据库,输入你在SQL

Server里的用户名和密码。

a4c26d1e5885305701be709a3d33442f.png

Click next , Then the screen shows OLE DB Property values ,

leave it as it is , and then click finish button.

选择“下一步”,就会弹出一个OLE DB Property的对话框。不改变任何东西,直接点击“完成”按钮。

a4c26d1e5885305701be709a3d33442f.png

After you click the finish button , the next window you will get

your Server name under OLEDB Connection, from there selected

database name (Crystaldb) and click the tables , then you can see

all your tables from your database.

点击“完成”按钮之后,下面的对话框中OLE

DB下就可以显示你刚才选择的服务器名称,选择Crystaldb,点击Table,你就会看到这个数据库中的所有表。

From the tables list double click the Product table then you can

see the Product table will come in the right side list.

在这些列出来的表中,双击Product表,它就会显示在右边。

a4c26d1e5885305701be709a3d33442f.png

Click Next Button

Select all fields from Product table to the right side list

.

点击“下一步”,选择Product表中的所有列。它们也将显示在右边。

a4c26d1e5885305701be709a3d33442f.png

Click Finish Button. Then you can see the Crystal Reports

designer window in your C# project. In the Crystal Reports designer

window you can see the selected fields from Product table. You can

arrange the field Objects and design of the screen according your

requirements. After that your screen is look like the following

picture.

点击“完成”按钮。你就会在C#项目里看到一个Crystal

Reports的设计窗口。这里你能看到刚才从Product表里选择的列名。你可以根据你的需要重新设计排列它们的位置。做完这些之后,你的报表看起来就类似与下图那样了:

a4c26d1e5885305701be709a3d33442f.png

Now the designing part is over and the next step is to call the

Crystal Reports in your C# application and view it through Crystal

Reports Viewer control in C#.

现在设计部分已经完成。下一步是在你的C#工程中调用Crystal Reports,而且通过Crystal Reports

Viewer来查看效果。

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

button and a CrystalReportViewer control to your form .

选择缺省的form(Form1.CS),你可以拖曳一个CrystalReportViewer控件到你的窗体中。

a4c26d1e5885305701be709a3d33442f.png

After you drag the CrystalReportViewer to your form , it will

look like the following picture.

它看起来可能像下图这样:

a4c26d1e5885305701be709a3d33442f.png

You have to include

CrystalDecisions.CrystalReports.Engine in your C# Source

Code.

在C#源代码中首先我们要加上这句

using CrystalDecisions.CrystalReports.Engine;

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

project

将下面的代码复制粘贴到你的c#工程中。

using System;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

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 file location,

there you can see CrystalReport1.rpt . So give the full path name

of Crystal Reports file like

c:\projects\crystalreports\CrystalReport1.rpt

When you run the source code you will get the report like the

following picture.

a4c26d1e5885305701be709a3d33442f.png

When you click the button, the application will ask the username

and password. Later in this tutorial you can find how to avoid

asking username and password - C# Dynamic logon parameters in Crystal Reports.

Hope this tutorial help you to create your first Crystal Reports in

C#.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值