水晶报表拉模式
最简单的一种写法
CrystalReportSource1.ReportDocument.Load(Server.MapPath("Report//*.rpt"));
// SetDatabaseLogon 拉模式中必须用这个方法来设置登录信息,参数一:用户名;参数二:密码;参数三:服务器;参数四:数据库名
CrystalReportSource1.ReportDocument.SetDatabaseLogon("report", "report", @"*", "database"); //给水晶报表传参数,参数一:是参数名,参数二:参数值; //CrystalReportSource1.ReportDocument.SetParameterValue("Title", "这是一个测试报表");
CrystalReportSource1.ReportDocument.SetParameterValue("BeginDate", txtDateBegin.Text.Trim());
//绑定水晶报表数据源。
CrystalReportSource1.DataBind(); // CrystalReportViewer1是水晶报表浏览器,下面是给该浏览器赋上对像
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.DataBind();
这种写法需填写登录数据的信息,太麻烦,以下这种可直接登录并传递参数
public void LoadReport1() {
//定义水晶报表的数据库连接信息
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = "*";
connectionInfo.DatabaseName = "Database";
connectionInfo.UserID = "report";
connectionInfo.Password = "**";
ReportDocument myReport = new ReportDocument();
string reportPath = Server.MapPath("Report\\*.rpt");
myReport.Load(reportPath);
//将数据库信息传递给报表
SetDBLogonForReport(connectionInfo, myReport);
myReport.SetParameterValue("BeginDate", txtDateBegin.Text.Trim());
myReport.SetParameterValue("EndDate", txtDateFrom.Text.Trim());
//把模板对象赋给报表前端呈现控件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);
}
}