Crystal Report提拱兩種資料存取模式:
- 提取模式
由報表引擎直接向資料庫提取資料。包括連線還有SQL都由Crystal Report處理。 - 推入模式
由開發人員自行撰寫連接資料庫與撈資料部分,再將資料結果(通常是DataSet)推入報表引擎,再顯示於報表。
當使用提取模式時,就需要另外處理報表連接資料庫時所需要的帳號密碼問題。因為安全考量,Crystal Report並不會記憶連接資料庫的帳號密碼。TableLonOnInfo就是提供來處理這個問題的。
1: protected void ConfigCrystalReport(){
2: TableLogOnInfo logOnInfo = new TableLogOnInfo();
3:
4: //load report
5: ReportDocument reportDoc = new ReportDocument();
6: reportDoc.Load(Server.MapPath("rpt_name.rpt"));
7:
8: //set up connection info for each table in report
9: //if the report connect to not only one table in db
10: foreach (CrystalDecisions.CrystalReports.Engine.Table table
11: in reportDoc.Database.Tables)
12: {
13: logOnInfo = table.LogOnInfo;
14:
15: //抓取config檔裡 <AppSettings>區段內設定好的資料
16: //除了下面列出的UserID, Password, 還可設定ServerName, DatabaseName
17: logOnInfo.ConnectionInfo.UserID =
18: ConfigurationManager.AppSettings["_UserId"];
19: logOnInfo.ConnectionInfo.Password =
20: ConfigurationManager.AppSettings["_Password"];
21:
22: // apply settings
23: table.ApplyLogOnInfo(logOnInfo);
24: }
25: }
Ref:
Post a Comment