December 19,2005
[ASP.Net]連結Crystal Report
Import CrystalDecisions.CrystalReports.Engine ( for ReportDocument物件)
CrystalDecisions.Shared ( for TableLogOnInfo 物件 )
code behind
using CrystalDecisions.Web;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
private void Page_Load(object sender, System.EventArgs e)
{
ReportDocument crReport = new ReportDocument();
Config.CrystalReportConnection(ref crReport,"CrystalReport1.rpt");
//第二個參數是Crystal Report報表檔名
crvReport.ReportSource=crReport; //crvReport is Crystal Report Viewer's ID
}
Config.cs
public static void CrystalReportConnection(ref CrystalDecisions.CrystalReports.Engine.ReportDocument crReport, string CrystalReportFileName)
{
crReport.Load(System.Web.HttpContext.Current.Server.MapPath(Config.getInstance().crystalReport_FileRoot) +"/"+ CrystalReportFileName);
//設定 *.rpt 檔案來源
//下述設定連結內容
TableLogOnInfo crLogOnInfo = new TableLogOnInfo();
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crReport.Database.Tables)
{
crLogOnInfo = crTable.LogOnInfo;
crLogOnInfo.ConnectionInfo.ServerName="server ip";
crLogOnInfo.ConnectionInfo.UserID="id";
crLogOnInfo.ConnectionInfo.Password="pwd";
crLogOnInfo.ConnectionInfo.DatabaseName="DB_name";
crTable.ApplyLogOnInfo(crLogOnInfo);
}
//上述設定連結內容
}