【问题标题】:Override DbConnectionAttributes in Crystal Report Viewer when using ADO.NET (XML) connection使用 ADO.NET (XML) 连接时覆盖 Crystal Report Viewer 中的 DbConnectionAttributes
【发布时间】:2020-09-15 17:25:57
【问题描述】:

我有一个带有水晶报表查看器的 .net 表单。我试图在其中加载的报告是使用 ADO.Net(xml) 类型的连接创建的,该连接具有指向返回数据集的文件名(在本例中为 .dll)的硬编码路径。我的问题是该 dll 的路径因应用程序的安装路径而异。所以我需要在代码中覆盖它,但我不知道该怎么做。这是我正在使用的代码:

Dim conInfo As New ConnectionInfo()
conInfo.Type = CrystalDecisions.Shared.ConnectionInfoType.CRQE
conInfo.Attributes.Collection.Add(New NameValuePair2("Database DLL", "crdb_adoplus.dll"))
conInfo.Attributes.Collection.Add(New NameValuePair2("QE_DatabaseName", ""))
Dim dba As New DbConnectionAttributes
dba.Collection.Add(New NameValuePair2("Class Name", "class name in that dll"))
dba.Collection.Add(New NameValuePair2("DataSet Names", "method in the class"))
dba.Collection.Add(New NameValuePair2("File Path", "path to dll.dll"))
conInfo.Attributes.Collection.Add(New NameValuePair2("QE_DatabaseType", "ADO.NET (XML)"))
conInfo.Attributes.Collection.Add(New NameValuePair2(DbConnectionAttributes.QE_LOGON_PROPERTIES, dba))
CrystalReportViewer1.ParameterFieldInfo = paramFields
CrystalReportViewer1.ReportSource = ReportFileName

Crystal 报告查看器出现并要求输入错误的登录信息。

任何帮助将不胜感激。

【问题讨论】:

    标签: vb.net crystal-reports ado.net crystal-reports-2008


    【解决方案1】:

    可能有点旧,但这里是 C# 和 Crystal Report 13.0.28 的有效解决方案。 无法判断是否真的需要修改 InitTable 中的其他属性。也许在未来的 CR 版本中不会将它们设置为这些默认值。

    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;
    
    private void PrintMe()
    {
        using (ReportDocument rep = new ReportDocument())
        {
            //Open report
            rep.Load(sReport, OpenReportMethod.OpenReportByTempCopy);
        
            NameValuePair2 nvp2 = new NameValuePair2("File Path ", sFullFileName);  //Space after "File Path " is important! The LogonProperties have defined it as such
            InitTables(rep, nvp2);
        
            rep.Refresh();
        
            if (sReportPdf == null)
            { 
                rep.PrintOptions.PaperSource        = printerSettings.PaperSource;
                rep.PrintOptions.PaperOrientation   = printerSettings.PaperOrientation;
                rep.PrintOptions.PaperSize          = printerSettings.PaperSize;
                rep.PrintOptions.PrinterName        = printerSettings.PrinterName;
                rep.PrintOptions.ApplyPageMargins(printerSettings.PageMargins);
        
                rep.PrintToPrinter(printerSettings.NumCopies, true, 1, 9999);
            }
            else
            {
                rep.ExportToDisk(ExportFormatType.PortableDocFormat, sReportPdf);
            }
        }
    }
    
    
    private void InitTables(ReportDocument rep, NameValuePair2 filePath)
    {
        foreach (Table table in rep.Database.Tables)
        {
            var collection = table.LogOnInfo.ConnectionInfo.Attributes.Collection;
    
            InitTableCollections(collection, "Database DLL", "crdb_adoplus.dll");   //same as default
            InitTableCollections(collection, "QE_DatabaseName", "");                //same as default
            InitTableCollections(collection, "QE_DatabaseType", "ADO.NET (XML)");   //same as default
            InitTableCollections(collection, "QE_ServerDescription", "items");      //default: invoice
            InitTableCollections(collection, "QE_SQLDB", false);                    //same as default
            InitTableCollections(collection, "SSO_Enabled", false);                 //same as default
    
            InitTableCollections(table.LogOnInfo.ConnectionInfo.LogonProperties, filePath);
    
            table.ApplyLogOnInfo(table.LogOnInfo);
        }
    
        try
        {
            foreach (ReportDocument subReportDocument in rep.Subreports)
                InitTables(subReportDocument, filePath);
        }
        catch (NotSupportedException)
        {
            //thrown when trying to enumerate rep.Subreports at a certain depth
        }
    }
    
    private void InitTableCollections(NameValuePairs2 collection, string key, object value)
    {
        if (collection.ContainsKey(key))
            collection.Set(key, value);
        else
            collection.Add(new NameValuePair2(key, value));
    }
    
    private void InitTableCollections(NameValuePairs2 collection, NameValuePair2 nvp)
    {
        if (collection.ContainsKey(nvp.Name))
            collection.Set(nvp.Name, nvp.Value);
        else
            collection.Add(new NameValuePair2(nvp.Name, nvp.Value));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 2023-03-19
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多