天天看點

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

<a href="http://www.codeproject.com/KB/reporting-services/WebAndReportingServices/WebAndReportingServices.zip">[原文源碼下載下傳]</a>

[翻譯]如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表

原文釋出日期:2006.05.18

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

介紹

好幾個月的時間了,我一直在學習Microsoft Reporting Services 2005的一些新的功能。其中之一就是如何使用webservice作資料源。但是很不幸,我無法在MSDN和SQL Server 2005的相關書籍中找到詳細的幫助資訊。是以我花了好長時間來搞定這個問題。希望通過分享我的Microsoft Reporting Services 2005的相關知識能夠節省你的開發時間。

建立一個webservice

第一步是建立一個webservice,稍後我将用這個webservice作為我的報表的資料源。這是非常重要的一步,因為我們要将資料轉換成一個XmlDataDocument。如果沒有這步轉換,我們将不能得到使用webservice的web方法取得資料的結果。實作這個轉換的C#代碼如下:

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

[WebMethod]

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

public XmlDataDocument GetPersonAddress(string cityNameID)

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

{

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   //

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   // 定義一些變量

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   //   

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   StringBuilder   myQuery           = new StringBuilder();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   XmlDataDocument resultXMLDocument = new XmlDataDocument();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   SqlConnection   myConnection      = new SqlConnection();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   SqlCommand      myCommand         = new SqlCommand();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   SqlDataAdapter  myDA              = new SqlDataAdapter();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   DataSet         myDS              = new DataSet();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   // 根據參數的不同準備不同的查詢語句

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   if ((cityNameID != null) &amp;&amp; (cityNameID.Trim() != ""))

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   {

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

    myQuery.Append("Select City as City, " + 

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

                   "AddressLine1 as Address, " + 

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

                   "PostalCode From Address ");

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

    myQuery.Append("Where City Like '" + 

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

                   cityNameID.Trim().Replace("%", "") + 

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

                   "%' Order By City");

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   }

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   else

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

    myQuery.Append("Select City as City, AddressLine1" + 

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

                   " as Address, PostalCode From Address" + 

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

                   " Order By City");

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   // 得到連接配接字元串并建立到伺服器的連接配接

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   myConnection.ConnectionString = ReadSetting("ConnectionString", "");

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   myCommand.Connection          = myConnection;

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   myCommand.CommandText         = myQuery.ToString();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   myCommand.CommandType         = CommandType.Text;

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   myDA.SelectCommand            = myCommand;

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   // 傳回一個DataSet資料

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   try

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

      myDA.Fill(myDS, "Address");

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

      //

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

      // 轉換我們的DataSet到XmlDataDocument

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

      XmlDataDocument temporaryXMLDoc = new XmlDataDocument(myDS);

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

      resultXMLDocument = temporaryXMLDoc;

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

      temporaryXMLDoc = null;

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

    }

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

    catch

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

    {

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

       resultXMLDocument = null;

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

    finally

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

       myDS.Dispose();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

       myDA.Dispose();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

       myCommand.Dispose();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

       myConnection.Dispose();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

       myQuery = null;

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

   return resultXMLDocument;

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

}

最後我們在IIS中釋出這個webservice,因為我們是使用VS2005開發的,如果你的電腦裡還裝了VS2003,那麼你應該檢查一下你的虛拟目錄關聯的服務是否是.net 2.0

使用這個webservice作為資料源建立和部署報表

我們的下一步就是用Microsoft Reporting Services 2005建立一個報表,其使用的資料源就是我們第一步所建立的那個webservice。要完成這個任務,你的電腦裡需要安裝Microsoft SQL Server 2005的Microsoft Reporting Services。我們先建立一個名為“TestReport”的報表伺服器項目。之後,我們添加一個共享資料源,将其類型設定為XML,連接配接字元串就是我們的webservice的位址。然後添加一個新報表,本例中webservice的命名空間是“http://madjarov_d_n_demo.org”,方法是“GetPersonAddress”

圖1

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

圖2

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

最後,你選擇下一步并生成報表。這樣報表就可以在SQL Server 2005和VS2005 Studio中設計了,圖例如下:

圖3

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

我們如何給webservice中的方法GetPersonAddress(string cityNameID)傳參數呢?微軟為開發人員提供了一個強大的報表引擎。我們可在Microsoft Reporting Services 2005中非常容易的設定它。首先我們要在設計模式中選擇報表參數,然後增加一個資料類型為“string”,名為“cityNameID”的參數,圖例如下:

圖4

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

最後一步就是把這個報表參數和DataSet關聯起來。為了達到這個目的,我們需要在設計模式中編輯資料源,首先建立一個DataSet,參數名為“cityNameID”,它的值為“Parameters!cityNameID.Value”,圖例如下:

圖5

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

現在我們就可以在報表伺服器中部署報表了。在部署之前你要確定你的“TargetReport Folder”和“TargetReport Server”被設定成了正确的值。你可以在報表屬性中設定它們,圖例如下:

圖6

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

現在你就可以把它部署到報表伺服器了。

給我們的報表建立一個簡單的視圖

我們最後的任務就是建立一個視圖程式,它負責從報表中擷取結果然後展現給我們。為了在這裡傳送一個參數到報表裡,我們在VS2005(C#)中建立了一個名為“TestReportWebViewer”的web站點,并把“Default.aspx”做我們web站點的預設頁,然後從工具箱裡把“ReportView”控件拖拽到我們的頁上。最後給它設定一個合适的大小并如下圖設定該控件其它的屬性。

圖7

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

請注意一定要把“ReportPath”和“ReportServerUrl”設定成我們之前部署報表時的相同的值,否則我們的“ReportView”控件将不會顯示我們的報表。下面是“ReportView”控件的“Init”事件的源代碼:

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

protected void rptViewer_Init(object sender, EventArgs e)

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

 //

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

 // 建立一個報表參數,并初始化它的值為“Al”

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

 //   

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

 ReportParameter cityID   = new ReportParameter();

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

 cityID.Name              = "cityNameID";

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

 cityID.Values.Add("Al"); 

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

 // 設定“ReportView”控件的處理模式為“Remote”

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

 rptViewer.ProcessingMode = ProcessingMode.Remote;

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

 // 傳送參數并初始化“ReportView”控件

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)
如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

 rptViewer.ServerReport.SetParameters(new ReportParameter[] { cityID });

如何使用webservice作為資料源去生成Microsoft Reporting Services 2005的報表(轉)

最後,謝謝你能看到這裡。希望當你嘗試在Microsoft Reporting Services 2005項目中使用webservice作資料源的時候本文能給你帶來一些幫助。你可以下載下傳本文提供的源碼仔細看其實作的過程。

本文轉自 你聽海是不是在笑 部落格園部落格,原文連結:  http://www.cnblogs.com/nuaalfm/archive/2009/02/16/1391619.html,如需轉載請自行聯系原作者