2011-11-23 68 views

回答

0

这是我管理的登录/密码的ConnectionStrings(dbLogin仅仅是用于存储信息的简单类,可以替换字符串值)。

//Somewhere in my code: 
    foreach (CrystalDecisions.CrystalReports.Engine.Table tbCurrent in rdCurrent.Database.Tables) 
     SetTableLogin(tbCurrent); 


    //My set login method 
    private void SetTableLogin(CrystalDecisions.CrystalReports.Engine.Table table) 
    { 
     CrystalDecisions.Shared.TableLogOnInfo tliCurrent = table.LogOnInfo; 

     tliCurrent.ConnectionInfo.UserID = dbLogin.Username; 
     tliCurrent.ConnectionInfo.Password = dbLogin.Password; 
     if(dbLogin.Database != null) 
      tliCurrent.ConnectionInfo.DatabaseName = dbLogin.Database; //Database is not needed for Oracle & MS Access 
     if(dbLogin.Server != null) 
      tliCurrent.ConnectionInfo.ServerName = dbLogin.Server; 
     table.ApplyLogOnInfo(tliCurrent); 
    } 
+0

感谢日志的朋友! –

0

如果你已经有了一个全成connectiont到SQL Server,你可以使用下面的静态方法根据您的SqlConnection设置报表连接:

using System.Text; 
using CrystalDecisions.Shared; 
using System.Data.SqlClient; 

namespace StackOverflow 
{ 
    public class MyCrystalReports 
    { 

     // This method will allow you may easily set report datasource based on your current SqlServerConnetion 
     public static void SetSqlConnection(CrystalDecisions.CrystalReports.Engine.ReportClass MyReport, SqlConnection MySqlConnection) 
     { 

      // You may even test SqlConnection before using it. 

      SqlConnectionStringBuilder SqlConnectionStringBuilder = new SqlConnectionStringBuilder(MySqlConnection.ConnectionString); 

      string ServerName = SqlConnectionStringBuilder.DataSource; 
      string DatabaseName = SqlConnectionStringBuilder.InitialCatalog; 
      Boolean IntegratedSecurity = SqlConnectionStringBuilder.IntegratedSecurity; 
      string UserID = SqlConnectionStringBuilder.UserID; 
      string Password = SqlConnectionStringBuilder.Password; 
      // Of course, you may add extra settings here :D 

      // On Crystal Reports, connection must be set individually for each table defined on the report document 
      foreach (CrystalDecisions.CrystalReports.Engine.Table Table in MyReport.Database.Tables) 
      { 

       CrystalDecisions.Shared.TableLogOnInfo TableLogOnInfo = Table.LogOnInfo; 

       TableLogOnInfo.ConnectionInfo.ServerName = ServerName; 
       TableLogOnInfo.ConnectionInfo.DatabaseName = DatabaseName; 
       TableLogOnInfo.ConnectionInfo.IntegratedSecurity = IntegratedSecurity; 

       if (IntegratedSecurity != true) 
       { 
        TableLogOnInfo.ConnectionInfo.UserID = UserID; 
        TableLogOnInfo.ConnectionInfo.Password = Password; 
       } 

       Table.ApplyLogOnInfo(TableLogOnInfo); 

      } 
     } 

    } 
}