1. 先建立一个只有资料行但没有连结资料库的dataset
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="">
-
<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>
4. 在.cs 加入以下代码
-
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;
-
-
}
-
}
5. 在textbox 输入id 及按button, 就可出现所选资料
阅读(91970) | 评论(0) | 转发(1) |