逐步解說:連接至 Web 服務中的資料 (Windows Form)

本逐步解說說明如何使用資料來源組態精靈將您的應用程式連接至 Web 服務。 您將連接至 Live Search Web 服務,並從應用程式執行搜尋查詢。 接著在 Windows Form 顯示從服務所傳回的資料 (搜尋結果)。

逐步解說將說明的工作包括:

  • 建立新的 [Windows 應用程式] 專案。

  • 將服務參考加入至應用程式 (連接至 Live Search 服務)。

  • 加入用來執行搜尋查詢的控制項 (服務所公開的呼叫方法)。

  • 撰寫存取服務及傳回資料的程式碼。

  • 將服務傳回的資料繫結至 BindingSource。

  • 在格線中顯示從服務傳回的資料。

取得 AppID

來自 Live Search 的 AppID 為免費提供,可對 Live Search 服務唯一識別您的應用程式。 AppID 為存取此服務所需。

若要取得 AppID

  • 巡覽至 http://search.live.com/developer 並取得免費 AppID

    注意事項注意事項

    AppID 可能需要一些時間 (30-60 分鐘) 才能完成服務驗證。如果您在這段時間內執行應用時遇到一般的「用戶端錯誤」,AppID 可能仍在 Live Search 伺服器上啟用中。

建立專案

若要建立新專案

  1. 從 [檔案] 功能表中,建立新專案。

  2. 選取 [Windows Form 應用程式],並為它命名為 WebServiceWalkthrough。

  3. 按一下 [確定]。

    建立專案並將其加入至 [方案總管]。

連接至服務

執行 [資料來源組態精靈] 來連接至 Web 服務。

若要連接至 Live Search Web 服務

  1. 按一下 [資料] 功能表上的 [顯示資料來源]。

  2. 在 [資料來源] 視窗中,選取 [加入新資料來源]。

  3. 選取 [選擇資料來源類型] 頁面上的 [服務],再按 [下一步]。

  4. 在 [加入服務參考] 對話方塊的 [URL] 方塊中,輸入 https://soap.search.msn.com/webservices.asmx?wsdl。

  5. 按一下 [移至]。

  6. 找到 Web 服務之後,將命名空間變更為:LiveSearchService。

  7. 按一下 [確定],然後按一下 [完成],將服務參考加入至專案。

    服務參考隨即加入至專案,而且 [資料來源] 視窗會根據服務所傳回的項目來填入。

注意事項注意事項

因為不同的 Web 服務會公開不同的功能,本逐步解說中的後續步驟是 Live Search Web 服務專用。若要使用從服務傳回的資料,一般程序是建立服務的執行個體,接著呼叫服務所公開的方法。從 [資料來源] 視窗拖曳項目之後,您的表單應該包含 BindingSource 元件;請將 DataSource 屬性設為服務所傳回的資料。

建立 DataGridView 以顯示從服務傳回的資料

從 [資料來源] 視窗將項目拖曳至表單上,藉以建立資料繫結的資料格。 加入格線之後,將資料行設定為只顯示您想要呈現的資料行。 接著將 Url 資料行設為連結,讓使用者可以按一下 URL 並巡覽至搜尋查詢所傳回的網站。

若要建立資料繫結的 DataGridView

  1. 在 [資料來源] 視窗中,展開 [SearchResponse] 節點。

  2. 展開 [Responses] 節點。

  3. 將 [Results] 節點拖曳至表單上。

    DataGridView、BindingSource 和 BindingNavigator 隨即加入至表單。

  4. 選取已加入至表單的 resultsDataGridView。

  5. 在 [屬性] 視窗中選取 [Columns] 屬性,然後按一下省略符號 (...) 開啟編輯資料行對話方塊 (設計檢視)

  6. 選取 [URL] 資料行並進行下列設定:

    1. ColumnType 屬性設為 DataGridViewLinkColumn

    2. AutoSizeMode 屬性設為 AllCells

  7. 除了 [標題]、[描述] 和 [Url] 資料行之外,移除所有資料行。

  8. 按一下 [確定]。

加入用來輸入搜尋準則及執行搜尋查詢的控制項

將用來執行搜尋查詢的控制項加入至現有的 Toolstrip。

若要將文字方塊和按鈕加入至表單

  1. 以滑鼠右鍵按一下表單 Toolstrip 上的灰色磁碟圖示,選取 [插入] 並接著選取 TextBox。

  2. 在 [屬性] 視窗中,將 [Name] 屬性設定為 searchCriteriaTextBox。

  3. 將 [Text] 屬性設定為 Visual Studio。

  4. 將 Button 控制項加入至 Toolstrip,並且將它命名為 searchButton。

  5. 在 [屬性] 視窗中,將 [DisplayStyle] 屬性設定為 Text

  6. 將 [Text] 屬性設定為 Search。

建立事件處理常式以開啟在格線中按一下的網站

加入 CellContentClick 事件的事件處理常式。

若要建立 CellContentClick 事件處理常式

  1. 選取表單上的 resultsDataGridView,然後按一下 [屬性] 視窗中的 [事件] 按鈕。 [事件] 按鈕是有閃電圖示的按鈕。

  2. 按兩下 [CellContentClick] 事件,建立並巡覽至處理常式 Stub。

  3. 加入程式碼,以檢查哪個資料行已按下並在按一下 Url 資料行時巡覽至網頁:

    Private Sub ResultsDataGridView_CellContentClick( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
        Handles ResultsDataGridView.CellContentClick
    
        ' When the content in a cell is clicked check to see if it is the Url column.
        ' If it is, pass the url to the Process.Start method to open the web page.
        If ResultsDataGridView.Columns(e.ColumnIndex).DataPropertyName = "Url" Then
            System.Diagnostics.Process.Start(ResultsDataGridView.SelectedCells(0).Value)
        End If
    End Sub
    
    private void resultsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        // When the content in a cell is clicked check to see if it is the Url column.
        // If it is, pass the url to the Process.Start method to open the web page.
        if (resultsDataGridView.Columns[e.ColumnIndex].DataPropertyName == "Url")
        {
            System.Diagnostics.Process.Start(resultsDataGridView.SelectedCells[0].Value.ToString());
        }
    }
    

加入用來存取 Live Search 服務及執行搜尋查詢的程式碼

藉由在應用程式具現化服務的執行個體並呼叫服務所公開的方法,來存取服務。

若要存取及使用服務

  1. 在程式碼編輯器中開啟 Form1。

  2. 將下列方法加入至 Form1:

    Private Sub RunSearchRequest()
    
        ' Create an instance of the service.
        Dim searchService As New LiveSearchService.MSNSearchPortTypeClient
    
        ' Instantiate a new SearchRequest.
        Dim searchRequest As New LiveSearchService.SearchRequest
    
        ' Create a new SourceRequest.
        Dim sourceRequest(1) As LiveSearchService.SourceRequest
        sourceRequest(0) = New LiveSearchService.SourceRequest
    
        ' To search the web, set the SourceType to Web.
        sourceRequest(0).Source = LiveSearchService.SourceType.Web
    
        ' Set the columns you want the query to return.
        sourceRequest(0).ResultFields = _
            LiveSearchService.ResultFieldMask.Description And _
            LiveSearchService.ResultFieldMask.Url And _
            LiveSearchService.ResultFieldMask.Title
    
        ' Search for the text in the textbox.
        searchRequest.Query = searchCriteriaTextBox.Text
    
        ' Set the SearchRequest to the SourceRequest array.
        searchRequest.Requests = sourceRequest
    
    
        ' Replace with a valid AppID. Obtain a free AppID at:
        ' http://search.live.com/developer 
        searchRequest.AppID = "AppID"
        searchRequest.CultureInfo = "en-US"
    
        ' Create a SearchResponse, then call the Search method
        ' and assign the return value to the response object.
        Dim searchResponse As LiveSearchService.SearchResponse = _
            searchService.Search(searchRequest)
    
        ' Bind the results to the form's BindingSource.
        ResultsBindingSource.DataSource = searchResponse.Responses(0).Results
    End Sub
    
    private void RunSearchRequest()
    {
        // Create an instance of the service.
        LiveSearchService.MSNSearchPortTypeClient searchService =
            new LiveSearchService.MSNSearchPortTypeClient();
    
        // Instantiate a new search request.
        LiveSearchService.SearchRequest searchRequest = new LiveSearchService.SearchRequest();
    
        // Create a new SourceRequest.
        LiveSearchService.SourceRequest[] sourceRequest = new LiveSearchService.SourceRequest[1];
        sourceRequest[0] = new LiveSearchService.SourceRequest();
    
        // Set the number of results to return.
        sourceRequest[0].Count = 7;
    
        // To search the web, set the SourceType to Web.
        sourceRequest[0].Source = LiveSearchService.SourceType.Web;
    
        // Set the columns to be returned from the search query.
        sourceRequest[0].ResultFields = LiveSearchService.ResultFieldMask.Description |
            LiveSearchService.ResultFieldMask.Url |
            LiveSearchService.ResultFieldMask.Title;
    
        // Set the search query to the value in the text box.
        searchRequest.Query = searchCriteriaTextBox.Text;
    
        // Set the search request to the array of source requests.
        searchRequest.Requests = sourceRequest;
    
        // Replace with a valid AppID. Obtain a free AppID at:
        // http://search.live.com/developer 
        searchRequest.AppID = "AppID";
        searchRequest.CultureInfo = "en-US";
    
        // Create a SearchResponse, then call the search method
        // and assign the return value to the response object.
        LiveSearchService.SearchResponse searchResponse = searchService.Search(searchRequest);
    
        // Bind the results from the search query to the form's BindingSource.
        resultsBindingSource.DataSource = searchResponse.Responses[0].Results;
    }
    
注意事項注意事項

務必將 searchRequest.AppID = "AppID" 取代成從 Live Search 服務取得的 AppID 值。

建立可在按一下搜尋按鈕時執行搜尋的事件處理常式

為 searchButton.Click 事件建立事件處理常式並呼叫 RunSearchRequest 方法。

若要在按一下按鈕時實作搜尋

  1. 在設計檢視中開啟 [Form1]。

  2. 按兩下 [搜尋] 按鈕。

  3. 在產生的處理常式中加入下列一行程式碼:

    Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
        RunSearchRequest()
    End Sub
    
    private void searchButton_Click(object sender, EventArgs e)
    {
        RunSearchRequest();
    }
    

執行應用程式

執行應用程式和搜尋。

若要執行應用程式和搜尋

  1. 執行應用程式 (按下 F5)。

  2. 按一下 [搜尋],在 Web 搜尋 Visual Studio (searchCriteriaTextBox 中的預設文字)。

    格線隨即顯示前十個搜尋結果。

  3. 按一下其中一個 URL,巡覽至該網站。

  4. 在文字方塊中輸入 Redmond WA Weather,然後按一下 [搜尋]。

    格線隨即以新的搜尋結果來更新。

後續步驟

存取 Live Search 服務的是做為起點,可以了解如何利用 Windows Form 應用程式中的資料繫結功能,顯示從服務傳回的資料。 連接至不同的服務以及從 [資料來源] 視窗拖曳項目之後,您的表單應該包含 BindingSource 元件;請將 DataSource 屬性設為服務所傳回的資料。 如需詳細資訊,請參閱 BindingSource 元件概觀

注意事項注意事項

[資料來源] 視窗中出現的項目視 Web 服務傳回的資訊而定。某些 Web 服務可能不會提供足夠的資訊,讓 [資料來源組態精靈] 建立可繫結的物件。例如,如果 Web 服務傳回未提供可搜尋之結構描述的物件,則完成精靈之後,在 [資料來源] 視窗中不會出現任何項目。

若要在應用程式中加入功能

請參閱

概念

將控制項繫結至 Visual Studio 中的資料

在您的應用程式中編輯資料

驗證資料

儲存資料

其他資源

資料逐步解說