Đầu tiên, ta tạo 1 database nhỏ để làm việc với nó, các bạn tạo trong sqlserver 1 database có tên example,
có 3 table là OrderMaster , OrderDetails ,Product
Code:
USE [example] GO /****** Object: Table [dbo].[OrderMaster] Script Date: 09/04/2009 15:11:59 ******/ SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[OrderMaster](
[OrderMaster_id] [int] NOT NULL,
[OrderMaster_date] [datetime] NULL,
[OrderMaster_customername] [varchar](50) NULL,
[OrderMaster_createduser] [varchar](50) NULL,
CONSTRAINT [PK_OrderMaster] PRIMARY KEY CLUSTERED (
[OrderMaster_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
GO /****** Object: Table [dbo].[OrderDetails] Script Date: 09/04/2009 15:11:59 ******/ SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[OrderDetails](
[OrderDetails_id] [int] NOT NULL,
[OrderDetails_masterid] [int] NULL,
[OrderDetails_productid] [int] NULL,
[OrderDetails_qty] [int] NULL,
CONSTRAINT [PK_OrderDetails] PRIMARY KEY CLUSTERED (
[OrderDetails_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO /****** Object: Table [dbo].[Product] Script Date: 09/04/2009 15:11:59 ******/ SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Product](
[Product_id] [int] NOT NULL,
[Product_name] [varchar](50) NULL,
[Product_price] [numeric](18, 0) NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED (
[Product_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
GO Vào menu PROJECT-->Add New Item. Chọn item là Crytal Report
Hiện ra 1 bảng chọn mẫu báo cáo, ở đây tôi sẽ để mặc định
Tiếp đến là chọn kết nối tới database ( ở đây là example), tôi sử dụng OLEDB Connection để kết nối.
Chọn OLEDB connection ở hôp thoại Create New Connection
Điền các thông tin kết nối vào :
Ở hộp thoại kế tiếp sẽ hiện các thông số kết nối, cứ việc Finish
Ở đây tôi muốn hiện thị table Product, nên chọn table Product
Chọn các trường của table product
Chọn next và chọn các kiểu hình thức của report, cuối cùng là Finish
Đã xong bước tạo report, trên Form1 chúng ta tạo lúc nãy,kéo vô 1 cái Button và 1 cái CrystalReportViewer control,khi click vô button thì sẽ hiện ra báo cáo :
Code:
ReportDocument cryRpt = new ReportDocument();
cryRpt.Load(@"C:\Documents and Settings\Qu@n Bom\My Documents\Visual Studio 2008\Projects\Example\Example\CrystalReport1.rpt");
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh(); Tạo Crystal Report với nhiều bản
Bài trên ta đã tạo report chỉ với 1 bảng là Product, bây giờ sẽ tạo report với nhiều bảng xem sao nhé
Mọi việc cũng giống như trên, đến bước chọn các table thì ta chọn cả 3 table cho nó máu
, sau đấy là chọn các fields muốn report :

Mọi chuyện lại tiếp tục như thế
Mọi việc cũng giống như trên, đến bước chọn các table thì ta chọn cả 3 table cho nó máu
Mọi chuyện lại tiếp tục như thế
No comments:
Post a Comment