使用ADO.NET(XML)连接时,在Crystal Report Viewer中覆盖DbConnectionAttributes

时间:2012-10-16 19:48:43

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

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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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));
}