2

在 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 查看器中时,如何设置连接字符串信息?

在此先感谢您的帮助...

4

2 回答 2

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
于 2012-06-26T05:19:25.580 回答
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;
于 2012-06-25T21:53:26.127 回答