如何使用ReportingService2010?

dca*_*iro 19 c# reporting-services

我正在尝试使用报告服务器Web服务按代码部署报告服务器解决方案:http://_Server_Name_/ReportServer/ReportService2010.asmx?wsdl.

可悲的是我在网上找不到任何例子.只有来自MSDN的一些模糊信息.

通过Business Intelligence Development Studio发布时,它会发布共享数据源,然后发布报告.我正试图在C#上做类似的事情:

var service = new ReportingService2010();
service.Credentials = new NetworkCredential(username, password, domain);

foreach(var dataSourcePath in GetDataSources()) {
    string name = Path.GetFileNameWithoutExtension(dataSourcePath);
    Byte[] content = GetFileContent(dataSourcePath);
    service.CreateCatalogItem("DataSource", name, parent, true, content, null, out warnings);
}
Run Code Online (Sandbox Code Playgroud)

但是CreateCatalogItem给了我以下SoapException异常:

输入XML不符合架构.API语法中描述了XML语法.对于报告中的XML,请参阅报告定义语言语法.---> Microsoft.ReportingServices.Diagnostics.Utilities.InvalidXmlException:输入XML不符合架构.API语法中描述了XML语法.对于报告中的XML,请参阅报告定义语言语法.

有什么我做错了或我应该采取的任何其他方法?

Dmi*_*try 11

我有同样的问题.我找到的解决方案如下:您使用了错误的DataSource文件格式 - 如下所示:

 <?xml version="1.0" encoding="utf-8"?>
 <RptDataSource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="DataSourceXML">
    <ConnectionProperties>
        <Extension>XML</Extension>
        <ConnectString>http://server/_vti_bin/lists.asmx</ConnectString>
        <IntegratedSecurity>true</IntegratedSecurity>
    </ConnectionProperties>
    <DataSourceID></DataSourceID>
</RptDataSource> 
Run Code Online (Sandbox Code Playgroud)

正确的是:

<?xml version="1.0" encoding="utf-8"?>
<DataSourceDefinition xmlns="http://schemas.microsoft.com/sqlserver/reporting/2006/03/reportdatasource">
  <Extension>XML</Extension>
  <ConnectString>http://server/_vti_bin/lists.asmx</ConnectString>
  <CredentialRetrieval>Prompt</CredentialRetrieval>
  <WindowsCredentials>True</WindowsCredentials>
  <Prompt></Prompt>
  <Enabled>True</Enabled>
</DataSourceDefinition>
Run Code Online (Sandbox Code Playgroud)

您可以通过从Reporting Server下载DataSource来获取此定义.


小智 5

这是一种从报表服务器中获取每个项目的XML的方法,从某种意义上说,这是一种从报表服务器"下载"任何对象(包括"DataSource")的XML定义的方法(假设您的报表服务器数据库是ReportServer):

select *, CONVERT(varchar(max),Content) as ContentText
from 
(
      SELECT 
     ItemID,Name,[Type],TypeDescription 
    , CASE 
      WHEN LEFT(Content,3) = 0xEFBBBF 
        THEN CONVERT(varbinary(max),SUBSTRING(Content,4,LEN(Content))) 
      ELSE 
        Content 
      END AS Content 
    from
    (
      SELECT 
         ItemID,Name,[Type] 
       , CASE Type 
          WHEN 2 THEN 'Report' 
          WHEN 5 THEN 'Data Source' 
          WHEN 7 THEN 'Report Part' 
          WHEN 8 THEN 'Shared Dataset' 
          ELSE 'Other' 
         END AS TypeDescription 
       , CONVERT(varbinary(max),Content) AS Content    
       FROM ReportServer.dbo.Catalog 
       WHERE Type IN (2,5,8)
    ) as ItemContentBinaries
) as ItemContentNoBOM
Run Code Online (Sandbox Code Playgroud)

对于SQL数据源,这是我们的定义:

<?xml version="1.0" encoding="utf-8"?>
<DataSourceDefinition xmlns="http://schemas.microsoft.com/sqlserver/reporting/2006/03/reportdatasource">
  <Extension>SQL</Extension>
  <ConnectString>Data Source=MyDatabaseServer;Initial Catalog=MyDatabase</ConnectString>
  <CredentialRetrieval>Integrated</CredentialRetrieval>
  <Enabled>True</Enabled>
</DataSourceDefinition>
Run Code Online (Sandbox Code Playgroud)

要记住的一件事是,我们找不到更改.rds文件的方法,并使其与报告IDE和自动部署一起使用.我们在Visual Studio 2008中使用.rptproj(Visual Studio 2010无法与Sql Server 2008 R2 Reporting Server项目一起使用).Visual Studio 2008要求DataSource文件(*.rds文件)采用旧的架构格式,这不适用于rs.exe和CreateCatalogItem.

如果我们将.rds文件转换为适用于CreateCatalogItem的格式,则在尝试打开.rptproj时,Sql Server 2008 R2 Reporting Server项目会出现以下错误:

Microsoft SQL Server报表设计器无法加载报表定义:XML文档中存在错误(2,2).验证报表定义是否符合正确的架构.XML文档中存在错误(2,2).(的System.Xml)

<DataSourceDefinition xmlns='http://schemas.microsoft.com/sqlserver/reporting/2006/03/reportdatasource'> was not expected. (wp6bqrt3)
Run Code Online (Sandbox Code Playgroud)


小智 4

我从未真正尝试过通过目录添加数据源,但我确实知道一种肯定有效的方法。您所需要做的就是创建一个与您要发布的报表中引用的数据源同名的数据源。以下是来自 MSDN 的使用 ReportingService2010 的示例:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

class Sample
{
static void Main(string[] args)
{
    ReportingService2010 rs = new ReportingService2010();
    rs.Url = "http://<Server Name>" + 
        "/_vti_bin/ReportServer/ReportService2010.asmx";
    rs.Credentials = 
        System.Net.CredentialCache.DefaultCredentials;

    string name = "AdventureWorks.rsds";
    string parent = "http://<Server Name>/Docs/Documents/";

    // Define the data source definition.
    DataSourceDefinition definition = new DataSourceDefinition();
    definition.CredentialRetrieval = 
        CredentialRetrievalEnum.Integrated;
    definition.ConnectString = 
        "data source=(local);initial catalog=AdventureWorks";
    definition.Enabled = true;
    definition.EnabledSpecified = true;
    definition.Extension = "SQL";
    definition.ImpersonateUserSpecified = false;
    //Use the default prompt string.
    definition.Prompt = null;
    definition.WindowsCredentials = false;

    try
    {
        rs.CreateDataSource(name, parent, false, 
            definition, null);
    }
    catch (SoapException e)
    {
        Console.WriteLine(e.Detail.InnerXml.ToString());
    }
}
}
Run Code Online (Sandbox Code Playgroud)

以下是发布报告和创建数据源的代码,虽然不是为 Reportingservice2010 编写的,但将其移至 2010 应该不难:

Byte[] definition = null;
Warning[] warnings = null;
string parentFolder = "AdventureWorks Sample Reports";
string parentPath = "/" + parentFolder;
string filePath = "D:\\Program Files\\Microsoft SQL Server\\100\\Samples\\Reporting Services\\Report Samples\\AdventureWorks Sample Reports\\";
public void Main()
{
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

//Create the parent folder
try {
    rs.CreateFolder(parentFolder, "/", null);
    Console.WriteLine("Parent folder {0} created successfully", parentFolder);
} catch (Exception e) {
    Console.WriteLine(e.Message);
}

//Publish the sample reports
PublishReport("EmbeddedDatasource");
}

public void PublishReport(string reportName)
{
try {
    FileStream stream = File.OpenRead(filePath + reportName + ".rdl");
    definition = new Byte[stream.Length + 1];
    stream.Read(definition, 0, Convert.ToInt32(stream.Length));
    stream.Close();
} catch (IOException e) {
    Console.WriteLine(e.Message);
}
try {
    warnings = rs.CreateReport(reportName, parentPath, false, definition, null);
    if ((warnings != null)) {
        Warning warning = default(Warning);
        foreach ( warning in warnings) {
            Console.WriteLine(warning.Message);
        }
    } else {
        Console.WriteLine("Report: {0} published successfully with no warnings", reportName);
    }
} catch (Exception e) {
    Console.WriteLine(e.Message);
}
try {
    DataSourceDefinition definition = new DataSourceDefinition();
    definition.CredentialRetrieval = CredentialRetrievalEnum.Store;
    DataSourceReference reference = new DataSourceReference();

    definition.ConnectString = "Data Source=.;Initial Catalog=AdventureWorks";
    definition.UserName = "username";
    definition.Password = "password";
    definition.Extension = "SQL";
    definition.WindowsCredentials = true;
    DataSource[] sources = new DataSource[1];
    DataSource s = new DataSource();
    s.Item = definition;
    s.Name = "DataSource1";
    sources(0) = s;
    rs.SetItemDataSources("/AdventureWorks Sample Reports/EmbeddedDatasource", sources);

} catch (Exception exp) {
    Console.WriteLine(exp.Message);
}
}
Run Code Online (Sandbox Code Playgroud)