摘要:[ASP.NET][C#]CrystalReport初學者教學-瀏覽時不用輸入帳密
1. 到工具箱內拖曳一個CrystalReportViewer到
WebForm1.aspx內
2. 將CrystalReportViewer內啟用資料庫登入提示,勾選取消
3. 打開WebForm1.aspx.cs 到 Page_Load 底下,編寫程式,讓網頁一打開就先載入執行
4. 程式碼
5. 程式原始碼
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ReportDocument cryRpt = new ReportDocument();
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
CrystalDecisions.Shared.ConnectionInfo crConnectionInfo = new CrystalDecisions.Shared.ConnectionInfo();
CrystalDecisions.CrystalReports.Engine.Tables CrTables;
cryRpt.Load(Server.MapPath("CrystalReport1.rpt"));
crConnectionInfo.ServerName = "127.0.0.1";
crConnectionInfo.DatabaseName = "TestDB";
crConnectionInfo.UserID = "sa";
crConnectionInfo.Password = "saPassword";
CrTables = cryRpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
crtableLogoninfo = CrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crtableLogoninfo);
}
CrystalReportViewer1.ReportSource = cryRpt;
CrystalReportViewer1.RefreshReport();
}
}
}