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

原文地址: http://www.codeproject.com/sqlrs/WebAndReportingServices.asp
[原文源码下载]


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


原文发布日期:2006.05.18
作者: Dimitar Madjarov
翻译: webabcd




介绍
好几个月的时间了,我一直在学习Microsoft Reporting Services 2005的一些新的功能。其中之一就是如何使用webservice作数据源。但是很不幸,我无法在MSDN和SQL Server 2005的相关书籍中找到详细的帮助信息。所以我花了好长时间来搞定这个问题。希望通过分享我的Microsoft Reporting Services 2005的相关知识能够节省你的开发时间。


新建一个webservice
第一步是新建一个webservice,稍后我将用这个webservice作为我的报表的数据源。这是非常重要的一步,因为我们要将数据转换成一个XmlDataDocument。如果没有这步转换,我们将不能得到使用webservice的web方法取得数据的结果。实现这个转换的C#代码如下:
None.gif [WebMethod]
None.gif
public  XmlDataDocument GetPersonAddress( string  cityNameID)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
//
InBlock.gif   
// 定义一些变量
InBlock.gif   
//   
InBlock.gif
   StringBuilder   myQuery           = new StringBuilder();
InBlock.gif   XmlDataDocument resultXMLDocument 
= new XmlDataDocument();
InBlock.gif   SqlConnection   myConnection      
= new SqlConnection();
InBlock.gif   SqlCommand      myCommand         
= new SqlCommand();
InBlock.gif   SqlDataAdapter  myDA              
= new SqlDataAdapter();
InBlock.gif   DataSet         myDS              
= new DataSet();
InBlock.gif
InBlock.gif   
InBlock.gif   
//
InBlock.gif   
// 根据参数的不同准备不同的查询语句
InBlock.gif   
//   
InBlock.gif
   if ((cityNameID != null&& (cityNameID.Trim() != ""))
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    myQuery.Append(
"Select City as City, " + 
InBlock.gif                   
"AddressLine1 as Address, " + 
InBlock.gif                   
"PostalCode From Address ");
InBlock.gif    myQuery.Append(
"Where City Like '" + 
InBlock.gif                   cityNameID.Trim().Replace(
"%"""+ 
InBlock.gif                   
"%' Order By City");
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    myQuery.Append(
"Select City as City, AddressLine1" + 
InBlock.gif                   
" as Address, PostalCode From Address" + 
InBlock.gif                   
" Order By City");
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
InBlock.gif   
//
InBlock.gif   
// 得到连接字符串并建立到服务器的连接
InBlock.gif   
//   
InBlock.gif
   myConnection.ConnectionString = ReadSetting("ConnectionString""");
InBlock.gif   myCommand.Connection          
= myConnection;
InBlock.gif   myCommand.CommandText         
= myQuery.ToString();
InBlock.gif   myCommand.CommandType         
= CommandType.Text;
InBlock.gif   myDA.SelectCommand            
= myCommand;
InBlock.gif
InBlock.gif   
InBlock.gif   
//
InBlock.gif   
// 返回一个DataSet数据
InBlock.gif   
//   
InBlock.gif
   try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      myDA.Fill(myDS, 
"Address");
InBlock.gif      
InBlock.gif      
//
InBlock.gif      
// 转换我们的DataSet到XmlDataDocument
InBlock.gif      
//
InBlock.gif
      XmlDataDocument temporaryXMLDoc = new XmlDataDocument(myDS);
InBlock.gif      resultXMLDocument 
= temporaryXMLDoc;
InBlock.gif      temporaryXMLDoc 
= null;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       resultXMLDocument 
= null;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       myDS.Dispose();
InBlock.gif       myDA.Dispose();
InBlock.gif       myCommand.Dispose();
InBlock.gif       myConnection.Dispose();
InBlock.gif       myQuery 
= null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif   
return resultXMLDocument;
ExpandedBlockEnd.gif}

最后我们在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


图2


最后,你选择下一步并生成报表。这样报表就可以在SQL Server 2005和VS2005 Studio中设计了,图例如下:
图3


我们如何给webservice中的方法GetPersonAddress(string cityNameID)传参数呢?微软为开发人员提供了一个强大的报表引擎。我们可在Microsoft Reporting Services 2005中非常容易的设置它。首先我们要在设计模式中选择报表参数,然后增加一个数据类型为“string”,名为“cityNameID”的参数,图例如下:
图4


最后一步就是把这个报表参数和DataSet关联起来。为了达到这个目的,我们需要在设计模式中编辑数据源,首先新建一个DataSet,参数名为“cityNameID”,它的值为“Parameters!cityNameID.Value”,图例如下:
图5


现在我们就可以在报表服务器中部署报表了。在部署之前你要确保你的“TargetReport Folder”和“TargetReport Server”被设置成了正确的值。你可以在报表属性中设置它们,图例如下:
图6


现在你就可以把它部署到报表服务器了。


给我们的报表创建一个简单的视图
我们最后的任务就是创建一个视图程序,它负责从报表中获取结果然后展现给我们。为了在这里传送一个参数到报表里,我们在VS2005(C#)中创建了一个名为“TestReportWebViewer”的web站点,并把“Default.aspx”做我们web站点的默认页,然后从工具箱里把“ReportView”控件拖拽到我们的页上。最后给它设置一个合适的大小并如下图设置该控件其它的属性。
图7


请注意一定要把“ReportPath”和“ReportServerUrl”设置成我们之前部署报表时的相同的值,否则我们的“ReportView”控件将不会显示我们的报表。下面是“ReportView”控件的“Init”事件的源代码:
None.gif protected   void  rptViewer_Init( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif 
//
InBlock.gif 
// 创建一个报表参数,并初始化它的值为“Al”
InBlock.gif 
//   
InBlock.gif
 ReportParameter cityID   = new ReportParameter();
InBlock.gif cityID.Name              
= "cityNameID";
InBlock.gif cityID.Values.Add(
"Al"); 
InBlock.gif 
InBlock.gif 
//
InBlock.gif 
// 设置“ReportView”控件的处理模式为“Remote”
InBlock.gif 
//   
InBlock.gif
 rptViewer.ProcessingMode = ProcessingMode.Remote;
InBlock.gif    
InBlock.gif 
//
InBlock.gif 
// 传送参数并初始化“ReportView”控件
InBlock.gif 
//   
ExpandedSubBlockStart.gifContractedSubBlock.gif
 rptViewer.ServerReport.SetParameters(new ReportParameter[] dot.gif{ cityID });
ExpandedBlockEnd.gif}

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

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

webabcd 2007-03-26 08:47 发表评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值