天天看点

[翻译]如何使用webservice作为数据源去生成Microsoft Reporting Services 2005的报表

原文地址:http://www.codeproject.com/KB/reporting-services/WebAndReportingServices.aspx

[原文×××]

[翻译]如何使用webservice作为数据源去生成Microsoft Reporting Services 2005的报表

原文发布日期:2006.05.18

作者:Dimitar Madjarov

翻译:webabcd

[翻译]如何使用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#代码如下:

[WebMethod]

public XmlDataDocument GetPersonAddress(string cityNameID)

{

     //

     // 定义一些变量

     //        

     StringBuilder     myQuery                     = new StringBuilder();

     XmlDataDocument resultXMLDocument = new XmlDataDocument();

     SqlConnection     myConnection            = new SqlConnection();

     SqlCommand            myCommand                 = new SqlCommand();

     SqlDataAdapter    myDA                            = new SqlDataAdapter();

     DataSet                 myDS                            = new DataSet();

     // 根据参数的不同准备不同的查询语句

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

     {

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

                                     "AddressLine1 as Address, " +    

                                     "PostalCode From Address ");

        myQuery.Append("Where City Like '" +    

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

                                     "%' Order By City");

     }

     else

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

                                     " as Address, PostalCode From Address" +    

                                     " Order By City");

     // 得到连接字符串并建立到服务器的连接

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

     myCommand.Connection                    = myConnection;

     myCommand.CommandText                 = myQuery.ToString();

     myCommand.CommandType                 = CommandType.Text;

     myDA.SelectCommand                        = myCommand;

     // 返回一个DataSet数据

     try

            myDA.Fill(myDS, "Address");

            //

            // 转换我们的DataSet到XmlDataDocument

            XmlDataDocument temporaryXMLDoc = new XmlDataDocument(myDS);

            resultXMLDocument = temporaryXMLDoc;

            temporaryXMLDoc = null;

        }

        catch

        {

             resultXMLDocument = null;

        finally

             myDS.Dispose();

             myDA.Dispose();

             myCommand.Dispose();

             myConnection.Dispose();

             myQuery = null;

     return resultXMLDocument;

}

最后我们在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”事件的源代码:

protected void rptViewer_Init(object sender, EventArgs e)

{    

//

// 创建一个报表参数,并初始化它的值为“Al”

//        

ReportParameter cityID     = new ReportParameter();

cityID.Name                            = "cityNameID";

cityID.Values.Add("Al");    

// 设置“ReportView”控件的处理模式为“Remote”

rptViewer.ProcessingMode = ProcessingMode.Remote;

// 传送参数并初始化“ReportView”控件

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

最后,谢谢你能看到这里。希望当你尝试在Microsoft Reporting Services 2005项目中使用webservice作数据源的时候本文能给你带来一些帮助。你可以下载本文提供的源码仔细看其实现的过程。

感谢我的同事Mr. Svilen Donev给我提供了很多有价值的信息。

继续阅读