如何在 Crystal XI R2 的 C# .NET 应用程序中以编程方式设置 Access 或 DBASE 文件的文件位置?

发布于 2024-10-07 17:09:09 字数 3004 浏览 8 评论 0原文

我需要能够在 .NET 应用程序中加载的 RPT 文件中为 Access 和 xBase 类型设置数据源。

我已经尝试过这个。

        CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument1 = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
        reportDocument1.Load(@"C:\MyCrystal.rpt", CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);

             string Constr = "MyConnectionString";
            // initialize an object to pull out the needed pieces for the login information.
            SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(ConStr);
            // Turn off integrated Security
            foreach (IConnectionInfo i in reportDocument1.DataSourceConnections)
            {
                i.IntegratedSecurity = false;
            }

            // Set the LogonInfo for each Data Source 
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in reportDocument1.Database.Tables)
            {
                TableLogOnInfo logOnInfo = CrTable.LogOnInfo;
                String dbtype = logOnInfo.ConnectionInfo.Attributes.Collection.LookupNameValuePair("QE_DatabaseType").Value.ToString().Trim();
                if (dbtype.Equals("xBase"))
                {
                    NameValuePairs2 lp = logOnInfo.ConnectionInfo.LogonProperties;
                    foreach (NameValuePair2 nvp in lp)
                    {
                        if (nvp.Name.ToString().Trim().Equals("Data File"))
                          nvp.Value = @"C:\MyDbaseFile.dbf";
                    }
                    CrTable.ApplyLogOnInfo(logOnInfo);
                }
                else if (dbtype.Equals("OLE DB (ADO)"))
                {
                    bool sql = true;
                    NameValuePairs2 lp = logOnInfo.ConnectionInfo.LogonProperties;
                    NameValuePair2 prov = lp.LookupNameValuePair("Provider");
                    if (!prov.Value.ToString().ToUpper().Equals("SQLOLEDB"))
                    {
                        sql = false;
                        NameValuePair2 ds = lp.LookupNameValuePair("Data Source");
                        ds.Value = @"C:\MyAccess.mdb";
                    }
                    if (sql)
                    {
                        // Set the connection information for the table in the report.
                        logOnInfo.ConnectionInfo.UserID = sb.UserID;
                        logOnInfo.ConnectionInfo.Password = sb.Password;
                        logOnInfo.ConnectionInfo.ServerName = sb.DataSource;
                        logOnInfo.ConnectionInfo.DatabaseName = sb.InitialCatalog;
                    } 
                    CrTable.ApplyLogOnInfo(logOnInfo);
                }
          }

        reportDocument1.VerifyDatabase();
    reportDocument1.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, @"C:\MyTest.pdf");

我在VerifyDatabase()上收到LogOnException(或者如果ExportToDisk不存在) 我已经转储了之前和之后的值,我看到了变化......但我一定做错了什么。请帮忙!

I need to be able to set a Data Source for both Access and xBase types in an RPT file I've loaded in a .NET Application.

I've tried this.

        CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument1 = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
        reportDocument1.Load(@"C:\MyCrystal.rpt", CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);

             string Constr = "MyConnectionString";
            // initialize an object to pull out the needed pieces for the login information.
            SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(ConStr);
            // Turn off integrated Security
            foreach (IConnectionInfo i in reportDocument1.DataSourceConnections)
            {
                i.IntegratedSecurity = false;
            }

            // Set the LogonInfo for each Data Source 
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in reportDocument1.Database.Tables)
            {
                TableLogOnInfo logOnInfo = CrTable.LogOnInfo;
                String dbtype = logOnInfo.ConnectionInfo.Attributes.Collection.LookupNameValuePair("QE_DatabaseType").Value.ToString().Trim();
                if (dbtype.Equals("xBase"))
                {
                    NameValuePairs2 lp = logOnInfo.ConnectionInfo.LogonProperties;
                    foreach (NameValuePair2 nvp in lp)
                    {
                        if (nvp.Name.ToString().Trim().Equals("Data File"))
                          nvp.Value = @"C:\MyDbaseFile.dbf";
                    }
                    CrTable.ApplyLogOnInfo(logOnInfo);
                }
                else if (dbtype.Equals("OLE DB (ADO)"))
                {
                    bool sql = true;
                    NameValuePairs2 lp = logOnInfo.ConnectionInfo.LogonProperties;
                    NameValuePair2 prov = lp.LookupNameValuePair("Provider");
                    if (!prov.Value.ToString().ToUpper().Equals("SQLOLEDB"))
                    {
                        sql = false;
                        NameValuePair2 ds = lp.LookupNameValuePair("Data Source");
                        ds.Value = @"C:\MyAccess.mdb";
                    }
                    if (sql)
                    {
                        // Set the connection information for the table in the report.
                        logOnInfo.ConnectionInfo.UserID = sb.UserID;
                        logOnInfo.ConnectionInfo.Password = sb.Password;
                        logOnInfo.ConnectionInfo.ServerName = sb.DataSource;
                        logOnInfo.ConnectionInfo.DatabaseName = sb.InitialCatalog;
                    } 
                    CrTable.ApplyLogOnInfo(logOnInfo);
                }
          }

        reportDocument1.VerifyDatabase();
    reportDocument1.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, @"C:\MyTest.pdf");

I get a LogOnException on the VerifyDatabase() (or if it's not there the ExportToDisk)
I've dumped out the values before and after and I see the change... but I must be doing something wrong. Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2024-10-14 17:09:09

从 SAP 支持人员那里得到了一些代码。

Got some code from an SAP support person for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文