共用方式為


第 3 課:存取 Web 服務

將報表伺服器 Web 服務的參考加入專案後,下一步就是建立 Web 服務之 Proxy 類別的執行個體。然後您可以藉由呼叫 Proxy 類別中的方法來存取 Web 服務的方法。當您的應用程式呼叫這些方法時,Visual Studio 產生的 Proxy 類別程式碼會處理您的應用程式與 Web 服務間的通訊。

首先,您要建立 Web 服務之 Proxy 類別的執行個體 ReportingService2005。下一步,您要使用 Proxy 類別來呼叫 Web 服務的 GetProperties 方法。您要使用此呼叫來擷取範例報表「公司銷售」的名稱和描述。

ms170088.note(zh-tw,SQL.90).gif附註:
當您想要存取在 SQL Server Express with Advanced Services 上執行的 Web 服務時,必須將 "$SQLExpress" 附加至 "ReportServer" 路徑。例如: http://<Server Name>/reportserver$sqlexpress/reportservice2005.asmx"

若要存取 Web 服務

  1. 您必須先將 using (Visual Basic 中的 Import) 指示詞加入程式碼檔案中,來將命名空間加入 Program.cs 檔 (Visual Basic 中的 Module1.vb)。如果您使用這個指示詞,就不需要完全符合命名空間的類型。

  2. 若要完成這個步驟,請在您的程式碼檔案開頭中加入以下的程式碼:

    Imports System
    Imports GetPropertiesSample.ReportService2005
    
    using System;
    using GetPropertiesSample.ReportService2005;
    
  3. 當您在程式碼檔案中輸入命名空間指示詞之後,請在主控台應用程式的主要方法中輸入下列程式碼。在設定 Web 服務執行個體的 Url 屬性時,請務必變更伺服器的名稱。

    Sub Main()
       Dim rs As New ReportingService2005
       rs.Credentials = System.Net.CredentialCache.DefaultCredentials
       rs.Url = "http://<Server Name>/reportserver/reportservice2005.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 Sample Reports/Company Sales", 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 Sub
    
    static void Main(string[] args)
    {
       ReportingService2005 rs = new ReportingService2005();
       rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
       rs.Url = "http://<Server Name>/reportserver/reportservice2005.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 Sample Reports/Company Sales",properties);
    
          foreach (Property p in returnProperties)
          {
             Console.WriteLine(p.Name + ": " + p.Value);
          }
       }
    
       catch (Exception e)
       {
          Console.WriteLine(e.Message);
       }
    }
    
  4. 儲存方案。

逐步解說範例程式碼使用 Web 服務的 GetProperties 方法來擷取範例報表「公司銷售」的屬性。GetProperties 方法需要兩個引數:您要擷取屬性資訊的報表名稱和包含之屬性名稱的值是您想擷取的 Property[] 物件陣列。這個方法也會傳回 Property[] 物件的陣列,其中包含屬性引數所指定的屬性名稱和值。

ms170088.note(zh-tw,SQL.90).gif附註:
如果您提供空的 Property[] 陣列給屬性引數,就會傳回所有可用的屬性。

在先前範例中,程式碼使用 GetProperties 方法來傳回範例報表「公司銷售」的名稱和描述。然後程式碼會使用 foreach 迴圈,將屬性和值寫入主控台。

如需有關建立和使用報表伺服器 Web 服務之 Proxy 類別的詳細資訊,請參閱<Creating the Web Service Proxy>。

請參閱

工作

第 4 課:執行應用程式 (VB/VC#)

概念

教學課程:利用 Visual Basic 或 Visual C# 來存取報表伺服器 Web 服務

說明及資訊

取得 SQL Server 2005 協助