將報表伺服器 Web 服務的參考新增至項目之後,下一個步驟是建立 Web 服務的 Proxy 類別實例。 接著,您可以呼叫 Proxy 類別中的 方法,以存取 Web 服務的方法。 當您的應用程式呼叫這些方法時,Visual Studio 所產生的 Proxy 類別程式代碼會處理應用程式與 Web 服務之間的通訊。
首先,您將建立 Web 服務的 Proxy 類別 ReportingService2010實例。 接下來,您將使用 Proxy 類別呼叫 Web 服務 GetProperties 的方法。 您將使用呼叫以擷取其中一個範例報表「公司銷售」的名稱和描述。
備註
使用進階服務存取在 SQL Server Express 上執行的 Web 服務時,您必須將 “$SQLExpress” 附加至 “ReportServer” 路徑。 例如:
http://<Server Name>/reportserver$sqlexpress/reportservice2010.asmx"
存取 Web 服務
您必須先在程式代碼檔案的 Program.cs 檔案(或在 Visual Basic 中為 Module1.vb)中新增命名空間,方法是將
using指示詞(在 Visual Basic 中為Imports)新增至程式代碼檔案。 如果您使用這個指示詞,就不需要完整限定命名空間中的類型。若要這樣做,請將下列程式代碼新增至程式代碼檔案的開頭:
Imports System Imports GetPropertiesSample.ReportService2010using System; using GetPropertiesSample.ReportService2010;一旦您輸入命名空間指示詞至程式碼檔案,請在主控台應用程式的 Main 方法中輸入下列程式代碼。 設定 Web 服務實例的 Url 屬性時,請務必變更伺服器的名稱:
Sub Main() Dim rs As New ReportingService2010 rs.Credentials = System.Net.CredentialCache.DefaultCredentials rs.Url = "http://<Server Name>/reportserver/reportservice2010.asmx" Dim name As New [Property] name.Name = "Name" Dim description As New [Property] description.Name = "Description" Dim properties(1) As [Property] properties(0) = name properties(1) = description Try Dim returnProperties As [Property]() = rs.GetProperties( _ "/AdventureWorks 2012 Sample Reports/Company Sales 2012", properties) Dim p As [Property] For Each p In returnProperties Console.WriteLine((p.Name + ": " + p.Value)) Next p Catch e As Exception Console.WriteLine(e.Message) End Try End Substatic void Main(string[] args) { ReportingService2010 rs = new ReportingService2010(); rs.Credentials = System.Net.CredentialCache.DefaultCredentials; rs.Url = "http://<Server Name>/reportserver/reportservice2010.asmx"; Property name = new Property(); name.Name = "Name"; Property description = new Property(); description.Name = "Description"; Property[] properties = new Property[2]; properties[0] = name; properties[1] = description; try { Property[] returnProperties = rs.GetProperties( "/AdventureWorks 2012 Sample Reports/Company Sales 2012",properties); foreach (Property p in returnProperties) { Console.WriteLine(p.Name + ": " + p.Value); } } catch (Exception e) { Console.WriteLine(e.Message); } }儲存組態。
逐步解說範例程式代碼會使用 GetProperties Web 服務的方法來擷取範例報表 Company Sales 2012 報表的屬性。 方法 GetProperties 會採用兩個自變數:您要擷取屬性資訊之報表的名稱,以及 Property[] 物件的陣列,其中包含您要擷取其值的屬性名稱。 方法也會傳回 Property[] 對象的陣列,其中包含 properties 自變數中所指定屬性的名稱和值。
備註
如果您為 properties 自變數提供空的 Property[] 陣列,則會傳回所有可用的屬性。
在上一個範例中,程式代碼會使用 GetProperties 方法來傳回範例報表公司銷售 2012 的名稱和描述。 程式代碼接著會使用 foreach 迴圈,將屬性和值寫入主控台。
如需建立及使用報表伺服器 Web 服務之 Proxy 類別的詳細資訊,請參閱 建立 Web 服務 Proxy。
另請參閱
第 4 課:執行應用程式 (VB-VC#)
使用 Visual Basic 或 Visual C# 存取報表伺服器 Web 服務 (SSRS 教學課程)