2012-06-25 29 views
2

在Visual Studio 2010中,我動態填充基於具有以下設置的XML文件的Crystal Reports的列表:更改Crystal報表查看連接字符串ASP.NET

<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" /> 

當報告鏈路上的用戶點擊(從樹節點對象配),則晶體報告觀看者報告被設置如下所示:

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

在我的實際RPT報告文件中,我保存了連接字符串,因此Crystal Report Viewer不會提示用戶輸入用戶名和密碼。

我的問題是要找出是否有可能更改保存在報告文件中的連接字符串?當我將一個RPT文件加載到Crystal Reports Viewer中時,如何設置連接字符串信息?

在此先感謝您的幫助......

回答

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; 
+0

哪裏crTableLogOnInfo定義? 這看起來不錯,感謝您的幫助,但是,如果每份報表有多個數據源,您是否知道該怎麼辦? 謝謝, –

+0

糟糕。編輯將'crTableLogOnInfo'定義爲'TableLogOnInfo'類型。我想,如果每份報告都有多個數據源,那麼您可以在編輯它時檢查每個數據源,並根據該信息決定現在指向何處。 – ScottieMc

+0

謝謝,我最好想定義一個web.config文件中的連接字符串,但這似乎並不支持。有沒有一種方法可以輕鬆定義我使用的是oracle還是Sql服務器,或者只是換出web.config文件中的連接字符串? –

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 
+0

再次,我在ASP.NET項目中嘗試了這一點,當我得到「沒有有效的報告來源可用」。儘管感謝您的幫助。 –