简体   繁体   中英

Changing a Crystal Report datasource (access) at runtime in C#

I'm getting the error 'Log on failed' when trying to change my datasource at runtime. I've trawled SO and the SAP site and from that put together the code below, but I'm still getting the error. My access db is 2013 and isn't password protected and I'm using the default "Admin" user. The report has an OLE DB connection. My code loops through all tables, including subreports and changes the login, and I also change the database login. Any help much appreciated:

public void RunTestReport()
    {
        DataSet testDataSet = new DL.NonConformance().GetNonConformances(1, "NonConformance");

        var rpt = new ReportDocument();
        rpt.Load(@"C:\Reports\rptTest.rpt");

        SetCrystalLogin(rpt);

        rpt.SetDataSource(testDataSet);

        crystalReportViewer1.ReportSource = rpt;
        crystalReportViewer1.Refresh();
    }

    public void SetCrystalLogin(ReportDocument oRpt)
    {
        ConnectionInfo oConnectInfo = new ConnectionInfo();

        string dbpath = string.Concat(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"\db\dbacc.db");

        oConnectInfo.ServerName = dbpath;
        oConnectInfo.DatabaseName = string.Empty;
        oConnectInfo.UserID = "Admin";
        oConnectInfo.Password = string.Empty;

        // Set the logon credentials for all tables
        SetCrystalTablesLogin(oConnectInfo, oRpt.Database.Tables);

        // Check for subreports
        foreach (CrystalDecisions.CrystalReports.Engine.Section oSection in oRpt.ReportDefinition.Sections)
        {
            foreach (CrystalDecisions.CrystalReports.Engine.ReportObject oRptObj in oSection.ReportObjects)
            {
                if (oRptObj.Kind == CrystalDecisions.Shared.ReportObjectKind.SubreportObject)
                {
                    // This is a subreport so set the logon credentials for this report's tables
                    CrystalDecisions.CrystalReports.Engine.SubreportObject oSubRptObj = oRptObj as CrystalDecisions.CrystalReports.Engine.SubreportObject;

                    // Open the subreport
                    CrystalDecisions.CrystalReports.Engine.ReportDocument oSubRpt = oSubRptObj.OpenSubreport(oSubRptObj.SubreportName);

                    SetCrystalTablesLogin(oConnectInfo, oSubRpt.Database.Tables);
                }
            }
        }

        oRpt.Refresh();

        oRpt.SetDatabaseLogon("Admin", string.Empty, dbpath, string.Empty, true);

        oRpt.VerifyDatabase();

        oRpt.Refresh();
    }

    private void SetCrystalTablesLogin(CrystalDecisions.Shared.ConnectionInfo oConnectInfo, Tables oTables)
    {
        foreach (CrystalDecisions.CrystalReports.Engine.Table oTable in oTables)
        {
            CrystalDecisions.Shared.TableLogOnInfo oLogonInfo = oTable.LogOnInfo;
            oLogonInfo.ConnectionInfo = oConnectInfo;

            oTable.ApplyLogOnInfo(oLogonInfo);
        }
    }

原来您无法在运行时配置.accdb数据源,我需要创建一个ODBC连接。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM