更改Crystal Reports Viewer连接字符串ASP.NET

时间:2012-06-25 21:19:40

标签: c# asp.net crystal-reports

在Visual Studio 2010中,我基于具有以下设置的XML文件动态填充Crystal Reports列表:

<Report file="C:\reportname.rpt"    text="Report Name"
       imageURL="~/images/reporticon.png" />

在我的ASPX页面中,我有一个CrystalReportsViewer,如下所示:

<CR:CrystalReportViewer ID="CrystalReportViewer" runat="server" AutoDataBind="true"
    Width="800px" Height="600px" CssClass="reportViewer"   HasCrystalLogo="False" />

当用户点击报告链接(来自TreeNode对象)时,水晶报告查看器报告的设置如下:

 CrystalReportViewer.ReportSource = "C:\reportname.rpt";

在我的实际RPT报告文件中,我保存了连接字符串,因此Crystal Report Viewer不会提示用户输入用户名和密码。

我的问题是找出是否可以更改报告文件中保存的连接字符串?将RPT文件加载到Crystal Reports Viewer时,如何设置连接字符串信息?

提前感谢您的帮助......

2 个答案:

答案 0 :(得分:0)

我不确定Web查看器是否与winforms查看器相同,但我最终要做的是创建一个新的ReportDocument,将报告文件加载到ReportDocument中,然后更改所有连接信息

ReportDocument currentReport = new ReportDocument();  
currentReport.Load([path to .rept], OpenReportMethod.OpenReportByTempCopy);
ConnectionInfo crConnectionInfo = new ConnectionInfo();

//Set your connection params here  

for (int i = 0; i < currentReport.Database.Tables.Count; i++)
{
    Table crTable = currentReport.Database.Tables[i];
    TableLogOnInfo crTableLogOnInfo = crTable.LogOnInfo;
    crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
    crTable.ApplyLogOnInfo(crTableLogOnInfo);
}

foreach(IConnectionInfo cn in currentReport.DataSourceConnections)
{
    cn.SetConnection(_server, _database, _windowsAuth);        
    cn.SetLogon(_userName, _password);
}  

然后只需将报表查看器源设置为报表对象:

CrystalReportViewer.ReportSource = currentReport;

答案 1 :(得分:0)

Private Sub RecurseAndRemap(ByVal CR As Engine.ReportDocument)
        For Each DSC As CrystalDecisions.Shared.IConnectionInfo In CR.DataSourceConnections
            DSC.SetLogon("YourUserName", "YourPassword")
            DSC.SetConnection("YouServerName", "YourDatabaseName", False)
        Next

        CR.SetDatabaseLogon("YourUserName", "YourPassword")

        For Each Table As Engine.Table In CR.Database.Tables
            Table.LogOnInfo.ConnectionInfo.UserID = "YourUserName"
            Table.LogOnInfo.ConnectionInfo.Password = "YourPassword"
        Next

        If Not CR.IsSubreport Then
            For Each SR As Engine.ReportDocument In CR.Subreports
                RecurseAndRemap(SR)
            Next
        End If
    End Sub