我想从 Crystal Report .rpt 文件中提取 SQL 查询,有没有办法做到这一点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/706128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:34:16  来源:igfitidea点击:

I would like to extract the SQL queries from Crystal Report .rpt files, is there a way to do this?

sqlcrystal-reportsreport

提问by Steve Stedman

I would like to extract the SQL queries from Crystal Report .rpt files, is there a way to do this?

我想从 Crystal Report .rpt 文件中提取 SQL 查询,有没有办法做到这一点?

I don't have any of the Crystal Reports products, just the .rpt files.

我没有任何 Crystal Reports 产品,只有 .rpt 文件。

采纳答案by Ed Harper

My experience is with older versions of Crystal (8,9) - I've no idea what the file formats look like for more recent versions. However, it's worth opening the files up in a text editor just in case, but for the file formats I've seen, the query text is not accessible this way.

我的经验是使用旧版本的 Crystal (8,9) - 我不知道更新版本的文件格式是什么样的。但是,为了以防万一,在文本编辑器中打开文件是值得的,但是对于我所看到的文件格式,无法通过这种方式访问​​查询文本。

If I remember correctly, some versions of Visual Studio 2003 came with tools for manipulating Crystal .rpt files (but I guess this isn't of much use to you, since if you had this already, you wouldn't be asking!).

如果我没记错的话,某些版本的 Visual Studio 2003 附带了用于操作 Crystal .rpt 文件的工具(但我想这对您没有多大用处,因为如果您已经有了它,您就不会问了!)。

It's not a very imaginative suggestion, but perhaps your quickest route would be to download the 30-day trial version of the current Crystal Reports, and see if that will open the files for you.

这不是一个非常有想象力的建议,但也许您最快的途径是下载当前 Crystal Reports30 天试用版,然后看看它是否会为您打开文件。

回答by JoshL

Here's a .Net example of code that grabs the Command Sql from all Crystal Reports in a given directory. It requires the Crystal 2008 .Net SDK to be installed (you can download a trial from SAP):

这是一个 .Net 代码示例,它从给定目录中的所有 Crystal Reports 中获取命令 Sql。它需要安装 Crystal 2008 .Net SDK(您可以从 SAP 下载试用版):

foreach (string file in Directory.GetFiles("c:\projects\Reports", "*.rpt"))
{
    Console.WriteLine(String.Format("Processing {0}...", file));
    var doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    doc.Load(file);

    foreach (dynamic table in doc.ReportClientDocument.DatabaseController.Database.Tables)
    {
        if (table.ClassName == "CrystalReports.CommandTable")
        {
            string commandSql = table.CommandText;

            //TODO: do something with commandSql
        }
    }
}

To get the SQL as Crystal would build it when running a report, see this link: SAP Note 1280515 - How to extract SQL query from Crystal reports using RAS sdk.

要像 Crystal 在运行报表时构建 SQL 那样获取 SQL,请参阅此链接:SAP 说明 1280515 - 如何使用 RAS sdk 从 Crystal 报表中提取 SQL 查询。

I believe to do this, you need to supply the report parameter values so that Crystal can connect to the database in order to build the SQL. In the example, since a Report Viewer control is used, Crystal can prompt the user for the parameters.

我相信要做到这一点,您需要提供报表参数值,以便 Crystal 可以连接到数据库以构建 SQL。在示例中,由于使用了报表查看器控件,Crystal 可以提示用户输入参数。

回答by craig

In "Crystal Reports ActiveX Designer Design and Runtime Library" (craxddrt.dll), the Report.SQLQueryString property will do what you want.

在“Crystal Reports ActiveX Designer Design and Runtime Library”(craxddrt.dll) 中,Report.SQLQueryString 属性将执行您想要的操作。

I can't seem to find an equivalent property in the .Net SDK, and believe me, I've been looking.

我似乎无法在 .Net SDK 中找到等效的属性,相信我,我一直在寻找。

** edit **

** 编辑 **

It appears that one can make use of the In-Process RAS Serverto get this information:

似乎可以使用In-Process RAS Server来获取此信息:

CrystalDecisions.ReportAppServer.DataDefModel.CommandTableClass.CommandText

CrystalDecisions.ReportAppServer.DataDefModel.CommandTableClass.CommandText

回答by Brandon

The other way around this is if you can run the reports, you can hook up SQL Profiler to your DB and capture the incoming SQL on the database side.

解决此问题的另一种方法是,如果您可以运行报告,则可以将 SQL Profiler 连接到您的数据库并在数据库端捕获传入的 SQL。

回答by j.wittwer

JoshL's answer worked for several of my reports, but not all of them. The following method, using ReportClientDocument.RowsetController.GetSQLStatement, was able to extract some of the queries that the other method missed.

JoshL 的回答对我的一些报告有效,但不是全部。以下方法使用ReportClientDocument.RowsetController.GetSQLStatement能够提取其他方法遗漏的一些查询。

foreach (string file in Directory.GetFiles("c:\projects\Reports", "*.rpt"))
{
    Console.WriteLine(String.Format("Processing {0}...", file));
    var doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    doc.Load(file);

    var controller = doc.ReportClientDocument.RowsetController;

    var groupPath = new CrystalDecisions.ReportAppServer.DataDefModel.GroupPath();
    string temp = String.Empty;
    string commandSql = controller.GetSQLStatement(groupPath, out temp);
    //TODO: do something with commandSql
}

回答by Farooq

In the Visual studio, select the .rpt file and Go to field explorer, right click on DatabaseFields. Click on SQL query option to view the query.

在 Visual Studio 中,选择 .rpt 文件并转到字段资源管理器,右键单击 DatabaseFields。单击 SQL 查询选项以查看查询。