2016-12-01 76 views
-1

裝入水晶報表,當我創建了一個水晶報表,可以運行正常,但需要輸入登錄信息。我GOOGLE了一下,找到這個代碼,但它並沒有爲我工作。任何與代碼建議將不勝感激。保存登錄信息在C#

private void frmPrintWIPReport_Load(object sender, EventArgs e) 
{ 
    var cryRpt = new ReportDocument(); 
    var crtableLogoninfo = new TableLogOnInfo(); 
    var crConnectionInfo = new ConnectionInfo(); 
    Tables CrTables; 

    cryRpt.Load("rptWIP.rpt"); 

    crConnectionInfo.ServerName = "192.168.40.253"; 
    crConnectionInfo.DatabaseName = "TNET"; 
    crConnectionInfo.IntegratedSecurity = true; 
    crConnectionInfo.UserID = "sa"; 
    crConnectionInfo.Password = "******"; 

    CrTables = cryRpt.Database.Tables; 
    foreach (Table CrTable in CrTables) 
    { 
     crtableLogoninfo = CrTable.LogOnInfo; 
     crtableLogoninfo.ConnectionInfo = crConnectionInfo; 
     CrTable.ApplyLogOnInfo(crtableLogoninfo); 
    } 

    crystalReportViewer1.ReportSource = cryRpt; 
    crystalReportViewer1.Refresh(); 
} 
+0

究竟 '不工作'?有什麼異常?錯誤信息?空行? – Martheen

+0

它需要登錄,當我們打開這個報告!我不需要登錄對話框出現! – user3035133

回答

0

你可以試試這個。我希望它和你的需要一樣。

private void frmPrintWIPReport_Load(object sender, EventArgs e) 
    { 
     var cryRpt = new ReportDocument(); 
     var crtableLogoninfo = new TableLogOnInfo(); 
     var crConnectionInfo = new ConnectionInfo(); 
     Tables CrTables; 

     cryRpt.Load("rptWIP.rpt"); 

     ConnectionInfo crConnectionInfo = new ConnectionInfo() 
      { 
       ServerName = @"192.168.40.253", 
       DatabaseName = @"TNET", 
       [email protected]"sa", 
       [email protected]"******"     
       //IntegratedSecurity = true 
      }; 

     CrTables = cryRpt.Database.Tables; 
     foreach (Table CrTable in CrTables) 
     { 
      crtableLogoninfo = CrTable.LogOnInfo; 
      crtableLogoninfo.ConnectionInfo = crConnectionInfo; 
      CrTable.ApplyLogOnInfo(crtableLogoninfo); 
     } 

     crystalReportViewer1.ReportSource = cryRpt; 
     crystalReportViewer1.Refresh(); 
    }