Crystal报表对ASP.Net运行时数据源的更改

时间:2017-03-16 03:56:31

标签: asp.net sql-server crystal-reports

有一个脚本通过ASP.Net运行水晶报告,导出并发送到电子邮件。

如果我在没有应用登录的情况下使用当前数据库,那么一切正常,但如果我在运行时更改数据源(相同的数据库结构但服务器不同),则问题如下。

Crystal 2008运行时

  

错误:System.Runtime.InteropServices.COMException(0x80042018):   表%1在文档中不存在。在   CrystalDecisions.ReportAppServer.Controllers.DatabaseControllerClass.VerifyTableConnectivity(对象   表)at   CrystalDecisions.CrystalReports.Engine.Table.TestConnectivity()at   ScriptCodeClass.ApplyLogon(ReportDocument cr,ConnectionInfo ci)at   ScriptCodeClass.Logon(ReportDocument cr,String server,String db,   ScriptCodeClass.FunCreatePDFView(String。中的String id,String pass)   lsHeader,String lsReportType,String msDatabaseUserId,String   msDatabasePassword)

此代码可以更改身份验证,但不能更改数据源/服务器,不知道是否需要引用或导入。

Imports System.Collections
Imports System.Data
Imports T1.Tb.Data
Imports System.IO
Imports System.Net
Imports System.Net.Mail
Imports T1.Tb
Imports T1.TB.Public
Imports CrystalDecisions.CrystalReports.Engine.ReportDocument
Imports CrystalDecisions.ReportSource
Imports System.Configuration
Imports System.Data.SqlClient


Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.Shared


References CrystalDecisions.CrystalReports.Engine
References CrystalDecisions.Shared
References System.Web.Services
References System.Data
References T1.Tb.dll
References T1.TB.Public
References T1.P1.dll
References T1.P1.Public
References T1.Tb.Fun


public shared function Logon(cr as ReportDocument, server as string, db as string, id as string, pass as string)  as Boolean
      'Use this to change the database logon info for a crystal report
      dim ci as ConnectionInfo = new ConnectionInfo()
      dim subObj as SubreportObject


      ci.ServerName = server
      ci.DatabaseName = db
      ci.UserID = id
      ci.Password = pass


      if ApplyLogon(cr, ci) then
        for each obj as ReportObject in cr.ReportDefinition.ReportObjects
           If (obj.Kind = ReportObjectKind.SubreportObject) Then
        //  if typeof obj.Kind.GetType() is CrystalDecisions.Shared.ReportObjectKind then
            subObj = ctype(obj, SubreportObject)
            if  not ApplyLogon(cr.OpenSubreport(subObj.SubreportName), ci) then
               return(false)
            end if
          end if
        next
        Logon = True
      end if
    end function


    private shared function ApplyLogon(cr as ReportDocument, ci as ConnectionInfo ) as Boolean


      dim li as TableLogOnInfo
      dim success as Boolean


      for each tbl as Table in cr.Database.Tables
        li = tbl.LogOnInfo
        li.ConnectionInfo = ci
        tbl.ApplyLogOnInfo(li)
        'check if logon was successful
        'if TestConnectivity returns false, check logon credentials
        if tbl.TestConnectivity() then
          'drop fully qualified table location
          if tbl.Location.IndexOf(".") > 0 then
            tbl.Location = tbl.Location.Substring(tbl.Location.LastIndexOf(".") + 1)
          else
            tbl.Location = tbl.Location     'THIS IS LINE LEFT OUT IN ALL SAMPLES I SAW
          end if
        else
          success = false
          exit for
        end if
        success = True
      next
    end function

2 个答案:

答案 0 :(得分:1)

首先尝试在代码中将查询构建为字符串,然后将其结果传递给水晶报表以获取报告并通过电子邮件发送。

步骤: 1)构建一个查询字符串。 2)执行该字符串并使用结果填充数据集内的数据表 3)使用该数据集/数据表在水晶报告中生成报告

答案 1 :(得分:1)

您有几种选择: 在web.config中添加密钥如下:

  <add key="ServerName" value=""/> Name Or IP Address
  <add key="DataBaseName" value=""/> Database Name
  <add key="DatabaseUser" value=""/>User Name
  <add key="DatabasePassword" value=""/>Password

并在加载或您的活动中在报告查看器中调用这些键:

    Dim SERVER_NAME As String = ConfigurationManager.AppSettings("ServerName").ToString()
    Dim DATABASE_NAME As String = ConfigurationManager.AppSettings("DataBaseName").ToString()
    Dim DatabaseUser As String = ConfigurationManager.AppSettings("DatabaseUser").ToString()
    Dim DatabasePassword As String = ConfigurationManager.AppSettings("DatabasePassword").ToString()

添加您的代码并登录到数据库

   CrystalReportViewer.SetDatabaseLogon(DatabaseUser, DatabasePassword, SERVER_NAME, DATABASE_NAME)

然后添加您的数据源:

CrystalReportViewer.SetDataSource

或者您可以将其直接传递给您的观众,如下所示:

    CrystalReportViewer.SetDatabaseLogon("sa", "123", "Your_Server", "YourDB")