1

我有一个带有水晶报表查看器的 .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 报表查看器出现并询问错误的登录信息。

任何帮助,将不胜感激。

4

1 回答 1

0

可能有点旧,但这是 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));
}
于 2020-09-15T09:25:57.157 回答