通过ReportService2005.asmx创建report并设置DataSource

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Web.Services.Protocols;
using ReportingService.ReportService;
using System.Xml;
using System.IO;

class Sample
{
    
public static string ConfigFile = "Config.xml";
    
public static void Main()
    {
        ReportingService2005 rs 
= new ReportingService2005();
        rs.Credentials 
= System.Net.CredentialCache.DefaultCredentials;
        XmlDocument xmlDoc 
= new XmlDocument();
        xmlDoc.Load(ConfigFile);
        
string parentFolder = xmlDoc.SelectSingleNode("//ParentFolder").InnerText;
        
//check whether have the parentFolder
        if (rs.GetItemType("/"+parentFolder)!=ItemTypeEnum.Folder)
        {
            
try
            {
                rs.CreateFolder(parentFolder, 
"/"null);
                Console.WriteLine(
"Parent folder {0} created successfully", parentFolder);
            }
            
catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        
        XmlNodeList nodeList 
= xmlDoc.SelectNodes("//ReportName");
        
foreach (XmlNode node in nodeList)
        {
            PublishReport(parentFolder, node.InnerText , rs, xmlDoc);
        }

        

    }
    
/// <summary>
    
/// Create Report and set the Data source
    
/// </summary>
    
/// <param name="parentFolder"></param>
    
/// <param name="reportName"></param>
    
/// <param name="rs"></param>
    
/// <param name="xmlDoc"></param>
    public static void PublishReport(string parentFolder, string reportName, ReportingService2005 rs, XmlDocument xmlDoc)
    {
        Byte[] definition 
= null;
        Warning[] warnings 
= null;
        
string filePath = xmlDoc.SelectSingleNode("//FilePath").InnerText;

          
try
          {
              
//read report definition
             FileStream stream = File.OpenRead(Path.Combine(filePath,reportName+".rdl"));
             definition 
= new Byte[stream.Length];
             stream.Read(definition, 
0, (int) stream.Length);
             stream.Close();
          }
          
catch(IOException e)
          {
             Console.WriteLine(e.Message);
          }

          
try
          {
              
//create report
             warnings = rs.CreateReport(reportName,"/"+parentFolder, false, definition, null);
             
//set report data source
             SetReportDataSources(parentFolder, reportName, xmlDoc, rs);


             
if (warnings != null)
             {
                 
foreach (Warning warning in warnings)
                 {
                     Console.WriteLine(warning.Message);
                 }
             }
             
else
             {
                 Console.WriteLine(
"Report: {0} created successfully with no warnings", reportName);
             }
          }
          
catch (SoapException e)
          {
             Console.WriteLine(e.Detail.InnerXml.ToString());
          }

    }

    
private static void SetReportDataSources(string parentFolder,string reportName,XmlDocument xmlDoc,ReportingService2005 rs)
    {
        
string dataSourceFolder=xmlDoc.SelectSingleNode("//DataSourceFolder").InnerText;
        
string dataSourceName = xmlDoc.SelectSingleNode("//DataSourceName").InnerText;

        DataSource[] dsarray;
        DataSourceReference reference 
= new DataSourceReference();
        DataSource ds 
= new DataSource();
        dsarray 
= new DataSource[1];
        reference.Reference 
= "/"+dataSourceFolder+"/"+dataSourceName;
        ds.Item 
= reference;
        ds.Name 
= dataSourceName;
        dsarray[
0= ds;
        
try
        {
            rs.SetItemDataSources(
"/" + parentFolder + "/" + reportName, dsarray);
            Console.WriteLine(
string.Format("Set {0} DataSource to {1}",reportName,dataSourceFolder+"/"+dataSourceName));
        }
        
catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        
    }



}

转载于:https://www.cnblogs.com/billmo/archive/2009/09/26/1574481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值