来自properties.settings的Crystal Report连接字符串winform c#

时间:2011-11-23 08:12:58

标签: c# crystal-reports

是否可以从c#中的winform的 properties.settings 设置水晶报表的连接字符串,如下所示?

这只是我的假设

rpt.connectionString = Properties.Settings.Default.ConnectionString

2 个答案:

答案 0 :(得分:0)

这是我管理登录/连接字符串的代码(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);
    }

答案 1 :(得分:0)

如果您已成功连接到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);

            }
        }

    }
}