日期:2014-05-20 浏览次数:20879 次
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using CrystalDecisions.Shared; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Web; public partial class _Default : System.Web.UI.Page { //水晶报表对象 private ReportDocument myReport; private void ConfigureCrystalReports() { // 定义ReportDocument对象,装载Crystalreport1.rpt myReport = new ReportDocument(); string reportPath = Server.MapPath("Crystalreport1.rpt"); myReport.Load(reportPath); //定义水晶报表的数据库连接信息 ConnectionInfo connectionInfo = new ConnectionInfo(); connectionInfo.DatabaseName = "user"; connectionInfo.UserID = "pwd"; connectionInfo.Password = "****"; connectionInfo.ServerName = "myoraServer"; //将数据库信息传递给报表 SetDBLogonForReport(connectionInfo, myReport); //把模板对象赋给报表前端呈现控件CrystalReportViewer1 CrystalReportViewer1.ReportSource = myReport; } private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument) { Tables tables = reportDocument.Database.Tables; foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables) { TableLogOnInfo tableLogonInfo = table.LogOnInfo; tableLogonInfo.ConnectionInfo = connectionInfo; table.ApplyLogOnInfo(tableLogonInfo); } } // 注意:将代码防到Page_Init或Page_Load 中 //VS2008必须放到Page_Init中 private void Page_Init(object sender, EventArgs e) { ConfigureCrystalReports(); } }
//主报表 myReport.SetDatabaseLogon("user", "pwd", "server", "db"); //子报表 myReport.Subreports[0].SetDatabaseLogon("user", "pwd", "server", "db");