2. 建立一个Crystal Report 并使用ProjectData > ADO.NET DataSet 连接刚才新开的DataSet
3.新增一个webform 并加入BUTTON ,CrystalReportViewer,Textbox
点击(此处)折叠或打开
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CrystalTest4.aspx.cs" Inherits="CrystalTest4" %>
- <%@ Register Assembly="CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
- <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
- </div>
-
- </form>
- </body>
- </html>
点击(此处)折叠或打开
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using CrystalDecisions.CrystalReports.Engine;
- using System.Data;
- using System.Data.SqlClient;
- using CrystalDecisions.Shared;
- public partial class CrystalTest4 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- ReportDocument rd = new ReportDocument();
- string reportPath = Server.MapPath("CrystalReport4.rpt");
- DataSet ds = InsertRecord();
- rd.Load(reportPath);
- rd.SetDataSource(ds);
- this.CrystalReportViewer1.ReportSource = rd;
-
- }
- private DataSet InsertRecord()
- {
- string constr = @"Data Source = 127.0.0.1; Initial Catalog = xxxx_DB; Persist Security Info = True; User ID = xxx_dbuser; Password =$xxxx";
- using (SqlConnection con = new SqlConnection(constr))
- {
- using (SqlCommand cmd = new SqlCommand("Select * from tbl_ole where ID='" + TextBox1.Text + "'"))
- {
- using (SqlDataAdapter sda = new SqlDataAdapter())
- {
- cmd.Connection = con;
- sda.SelectCommand = cmd;
- using (DataSet ds = new DataSet())
- {
- sda.Fill(ds, "DataTable1");
- return ds;
- }
- }
- }
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
-
- ReportDocument rd = new ReportDocument();
- string reportPath = Server.MapPath("CrystalReport4.rpt");
-
- DataSet ds = InsertRecord();
- rd.Load(reportPath);
- rd.SetDataSource(ds);
-
- this.CrystalReportViewer1.ReportSource = rd;
-
- }
- }