【參考組件】
CrystalDecisions.CrystalReports.Engine.dll
引用 ReportDocument物件
CrystalDecisions.Shared.dll
設定參數時需使用到的列舉型別
當使用 CrystalReportViewer 物件模型登入安全 SQL Server 資料庫時
◎ 要查看該專案所參考到的組件:

1. My Project 上按右鍵點選「開啟」,或於該專案上按右鍵點選「屬性」。
2. 選取「參考」頁籤。
請參考:Report Document 物件模型 (如何加入參考)
【匯入命名空間】
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
◎ 要查看或加入該專案所匯入的命名空間:
請參考:Crystal Reports for Visual Studio 2005 類別程式庫 命名空間
Imports CrystalDecisions.CrystalReports.Engine
Dim ReportDoc As ReportDocument = New ReportDocument
' 載入報表 ReportDoc.Load(Application.StartupPath + "\Report1.rpt")
' 顯示報表結果 CRViewer.ReportSource = ReportDoc
|
◎ 報表內資料庫登入資訊、參數設定
Imports CrystalDecisions.CrystalReports.Engine
Dim ReportDoc As ReportDocument = New ReportDocument Dim logonInfo As New TableLogOnInfo Dim table As Table
' 載入報表 ReportDoc.Load(Application.StartupPath + "\Report1.rpt")
' 設定報表內SQL Server資料庫的登入資訊 For Each table In ReportDoc.Database.Tables logonInfo = table.LogOnInfo With logonInfo.ConnectionInfo .ServerName = "localhost" .DatabaseName = "testDB" .IntegratedSecurity = False (若設定 True,則可以使用者作業系統 Windows 帳號登入資料庫,即不需指定 UserID 及 Password) .UserID = "abc" .Password = "1234" End With table.ApplyLogOnInfo(logonInfo) Next (也可針對不同資料表,動態設定不同的登入連線資訊)
' 設定報表內Access資料庫檔案 (若有需要只需指定ServerName、UserID、Password) For Each table In ReportDoc.Database.Tables logonInfo = table.LogOnInfo With logonInfo.ConnectionInfo .ServerName = Application.StartupPath + "\abc.mdb" (Access資料檔所在的完整路徑) End With table.ApplyLogOnInfo(logonInfo) Next
' 設定報表內ODBC資料來源名稱定義 (若有需要只需指定ServerName、UserID、Password) For Each table In ReportDoc.Database.Tables logonInfo = table.LogOnInfo With logonInfo.ConnectionInfo .ServerName = "test database" .UserID = "abc" .Password = "1234" End With table.ApplyLogOnInfo(logonInfo) Next
ReportDoc.Refresh()
' 設定報表參數 (離散值) ReportDoc.SetParameterValue("@ParameterStr", "1234") ReportDoc.SetParameterValue("@ParameterInt", 1234) ReportDoc.SetParameterValue("@ParameterBoolean", True) ReportDoc.SetParameterValue("@ParameterDate", "1/2/2009") ReportDoc.SetParameterValue("@ParameterTime", "1:12:23 PM") ReportDoc.SetParameterValue("@ParameterDateTime", CDate("1/2/2009 1:12:23 PM"))
' 設定報表參數 (多重值) Dim Values as New ParameterValues Dim p as ParameterDiscreteValue
P = New ParameterDiscreteValue p.Value = "AAA" Values.Add(p) P = New ParameterDiscreteValue
p.Value = "BBB"
Values.Add(p) ReportDoc.DataDefinition.ParameterFields("@ParameterStr".ApplyCurrentValues(Values)
' 設定報表參數 (範圍值) Dim RangeValue as New ParameterRangeValue
RangeValue.StartValue = New DateTime(2009, 1, 1) RangeValue.LowerBoundType = RangeBoundType.BoundExclusive (起始值不含) RangeValue.EndValue = New DateTime(2009, 1, 31) RangeValue.UpperBoundType = RangeBoundType.BoundInclusive (結束值有含) ReportDoc.SetParameterValue("@ParameterRange", RangeValue)
' 顯示報表結果 CRViewer.ReportSource = ReportDoc
|
請參考:ReportDocument 物件模型教學課程