2012-07-13 36 views
4

我正在生成一個報告,但問題是即使我提供了憑據,當包含CrystalReport的表單打開時,它仍然要求我給他們,最糟糕的部分是,我沒有輸入任何內容在那裏,只需點擊完成,然後加載報告。所以,如果不需要證書(或其他),爲什麼它會問我?Crystal報告,即使提供了詳細信息後,它爲什麼要求數據庫登錄?

下面的代碼

private void MainReport_Load(object sender, EventArgs e) 
    { 
     var constr = string.Empty; 
     constr = Application.StartupPath; 
     if (Generate.bForProjects) 
      constr = Path.Combine(constr, @"Reports\Projects.rpt"); 
     else 
      constr = Path.Combine(constr, @"Reports\Other.rpt"); 

     var myConInfo = new CrystalDecisions.Shared.TableLogOnInfo(); 
     reportDocument1.Load(constr); 
     myConInfo.ConnectionInfo.DatabaseName = "ProjectData.mdb"; 
     myConInfo.ConnectionInfo.ServerName = Application.StartupPath + @"\Data\ProjectData.mdb"; 
     myConInfo.ConnectionInfo.Password = ""; 
     myConInfo.ConnectionInfo.UserID = ""; 
     reportDocument1.Database.Tables[0].ApplyLogOnInfo(myConInfo); 

     reportDocument1.Refresh(); 

     crystalReportViewer1.ReportSource = reportDocument1; 
     crystalReportViewer1.Width = this.Width - 50; 
     crystalReportViewer1.Height = this.Height - 100; 
    } 

加載窗體時,此屏幕彈出窗口

enter image description here

而且,當這來了,我不輸入任何內容!那就對了!我只需點擊完成,即可完美加載報告!所以,如果它不需要任何東西,爲什麼hel *要求我登錄?

回答

2

當我們將一個水晶報表連接到另一個數據庫時,我們遇到了很多問題。我們最終創建了以下功能來正確設置它。它遞歸地設置所有報表和子報表上的連接。

另外我似乎記得我們有問題,如果報告是設計與集成Windows身份驗證,並運行一個簡單的用戶名和密碼。所以我們總是確保我們用簡單的用戶名和密碼連接到數據庫來設計報告。

private static void SetConnection(ReportDocument report, string databaseName, string serverName, string userName, string password) 
{ 
    foreach (Table table in report.Database.Tables) 
    { 
     if (table.Name != "Command") 
     { 
      SetTableConnectionInfo(table, databaseName, serverName, userName, password); 
     } 
    } 

    foreach (ReportObject obj in report.ReportDefinition.ReportObjects) 
    { 
     if (obj.Kind != ReportObjectKind.SubreportObject) 
     { 
      return; 
     } 

     var subReport = (SubreportObject)obj; 
     var subReportDocument = report.OpenSubreport(subReport.SubreportName); 
     SetConnection(subReportDocument, databaseName, serverName, userName, password); 
    } 
} 

private static void SetTableConnectionInfo(Table table, string databaseName, string serverName, string userName, string password) 
{ 
    // Get the ConnectionInfo Object. 
    var logOnInfo = table.LogOnInfo; 
    var connectionInfo = logOnInfo.ConnectionInfo; 

    // Set the Connection parameters. 
    connectionInfo.DatabaseName = databaseName; 
    connectionInfo.ServerName = serverName; 
    connectionInfo.Password = password; 
    connectionInfo.UserID = userName; 
    table.ApplyLogOnInfo(logOnInfo); 

    if (!table.TestConnectivity()) 
    { 
     throw new ApplicationException(Resource.UnableToConnectCrystalReportToDatabase); 
    } 

    table.Location = Database + "." + "dbo" + "." + table.Location; 
} 
+0

我試圖使用你創建的這些方法,他們不工作。它繼續問我舊數據庫,它永遠不會改變。你有什麼建議嗎? – 2013-08-20 03:06:36

1

只需創建報表對象,並添加以下鏈接 rptobj是crystalreport對象,setdatabaselogin是方法,採取用戶ID和密碼作爲參數。

rptobj.setdatabaselogon(userid,password)

0

當你的數據集或數據表是空的,只出現。所以確保它有數據。

相關問題