在后台:
protected void Page_Load(object sender, EventArgs e)
{
BindReport();
}
private void BindReport()
{
DataSet dsCryShui = new DataSet();
TicOA.BLL.SF_SHUI ShuiBll = new TicOA.BLL.SF_SHUI();
dsCryShui = dsCryShui = ShuiBll.GetListShuiAndShuiSys("");
ReportDocument redoc = new ReportDocument();
string reportLocalPath = "/CrystalReportShui.rpt";
//获取程序根目录
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());
//转换成绝对路径
string reportPath = tmpRootDir + reportLocalPath.Replace(@"/", @"\");
//读取Report
redoc.Load(reportPath);
redoc.SetDataSource(dsCryShui);
#region 设置缺省的数据库连接属性(这样每次都不需要填写登录信息)
TableLogOnInfo logOnInfo = new TableLogOnInfo();
//从web.config中获取logOnInfo参数信息
string ServerName, DatabaseName, UserID, Password;
//获取ServerName
ServerName = System.Configuration.ConfigurationManager.AppSettings["ServerName"];
//获取DatabaseName
DatabaseName = System.Configuration.ConfigurationManager.AppSettings["DatabaseName"];
//获取UserId
UserID = System.Configuration.ConfigurationManager.AppSettings["UserID"];
//获取password
Password = System.Configuration.ConfigurationManager.AppSettings["Password"];
//设置logOnInfo参数
logOnInfo.ConnectionInfo.ServerName = ServerName;
logOnInfo.ConnectionInfo.DatabaseName = DatabaseName;
logOnInfo.ConnectionInfo.UserID = UserID;
logOnInfo.ConnectionInfo.Password = Password;
redoc.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
#endregion
this.CrystalReportShui.ReportSource = redoc;
this.CrystalReportShui.HasCrystalLogo = false;
}
在前台 <CR:CrystalReportViewer ID="CrystalReportShui" runat="server" AutoDataBind="true"
Width="100%" Height="100%" />