水晶报表连接字符串从属性.设置winformc#

本文关键字:设置 winformc# 从属性 字符串 报表 连接 水晶 | 更新日期: 2023-09-27 18:15:41

是否可以从属性中设置水晶报表的连接字符串?c#中winform的设置如下?

这只是我的假设

rpt.connectionString = Properties.Settings.Default.ConnectionString

水晶报表连接字符串从属性.设置winformc#

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

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