Crystal reports 如何在Crystal Reports文件的命令表中动态正确地更改查询?

Crystal reports 如何在Crystal Reports文件的命令表中动态正确地更改查询?,crystal-reports,Crystal Reports,我有很多rpt文件。我想使用C#更改每个报表的查询。有几种方法可以实现这些更改 第一种方式: 这不是最好的办法。当查询具有任何参数时,方法SetSQLCommand不起作用。即使为每个参数设置了值,SetSQLCommand也不起作用。具有不起作用的参数的示例: private void button_Test_Click(object sender, EventArgs e) { ReportDocument rptDoc = new ReportDocument(); rpt

我有很多rpt文件。我想使用C#更改每个报表的查询。有几种方法可以实现这些更改

第一种方式: 这不是最好的办法。当查询具有任何参数时,方法SetSQLCommand不起作用。即使为每个参数设置了值,SetSQLCommand也不起作用。具有不起作用的参数的示例:

private void button_Test_Click(object sender, EventArgs e)
{
    ReportDocument rptDoc = new ReportDocument();
    rptDoc.Load("D:\\Temp_01\\Report1_Test.rpt");
    rptDoc.SetDatabaseLogon("User", "Password", "ServName", "DBName");
    CrystalDecisions.Shared.ConnectionInfo ConnInf;
    ConnInf = rptDoc.Database.Tables[0].LogOnInfo.ConnectionInfo;
    String strSQLQuery = "SELECT TOP(1) * FROM sys.all_objects WHERE name = {?strName}";
    String strTableName = rptDoc.Database.Tables[0].Name;
    try
    {
        rptDoc.SetParameterValue("strName", "Text");
        rptDoc.SetSQLCommandTable(ConnInf, strTableName, strSQLQuery);
        rptDoc.VerifyDatabase();
    }
    catch (Exception ex) { rptDoc.Close(); }
    rptDoc.SaveAs("D:\\Temp_02\\Report2_Test.rpt");
    rptDoc.Close();
}
它返回一个错误。此方法不适用于参数

第二种方式: 这也不是最好的办法。SetLocalTableEx方法执行的报告结构不正确。在运行SetLocalTableEx之后,属性ConnectionInf.UserId的值为NULL,同时也是连接的名称

在SetTableLocationEx之后:

rcd.DatabaseController.SetTableLocationEx(rTblOld, rTblNew);
String UserID;
UserID = rptDoc.Database.Tables[0].LogOnInfo.ConnectionInfo.UserID;
if (UserID == null) MessageBox.Show("UserID has NULL");
UserId的值为NULL

另外,在运行SetTableLocationEx之前,连接名称为MSODBCSQL11

运行SetTableLocationEx后,连接名为Command

所以, 如何在CommandTable for Crystal Reports文件中动态正确地更改查询

谢谢,
Artem

您正在
Crystal Report
中使用命令,这是从
数据库
Crystal Report
执行和显示数据的最佳方式,但不幸的是,您在代码隐藏中执行此操作

我的问题是:

您为什么不使用
Crystal Report
本身来执行此操作


有关详细信息,请参见此。

您正在
Crystal Report
中使用命令,这是执行和显示从
数据库到
Crystal Report
的数据时的最佳方式,但不幸的是,您是在代码隐藏中执行此操作的

我的问题是:

您为什么不使用
Crystal Report
本身来执行此操作

有关更多信息,请参见此

private void button_Test_Click(object sender, EventArgs e)
{
    ReportDocument rptDoc = new ReportDocument();
    rptDoc.Load("D:\\Temp_01\\Report1_Test.rpt");
    rptDoc.SetDatabaseLogon("User", "Password", "ServName", "DBName");
    ISCDReportClientDocument rcd = null;
    rcd = rptDoc.ReportClientDocument as ISCDReportClientDocument;
    CommandTable rTblOld;
    CommandTable rTblNew;
    rTblOld = rcd.Database.Tables[0] as CommandTable;
    rTblNew = rcd.Database.Tables[0].Clone(true) as CommandTable;
    rTblNew.CommandText = "SELECT TOP(1) * FROM sys.all_objects";
    try
    {
        rcd.DatabaseController.SetTableLocationEx(rTblOld, rTblNew);
        rcd.VerifyDatabase();
    }
    catch (Exception ex) { rcd.Close(); }
    rcd.SaveAs(rcd.DisplayName, "D:\\Temp_02\\", 1);
    rcd.Close();
}
rcd.DatabaseController.SetTableLocationEx(rTblOld, rTblNew);
String UserID;
UserID = rptDoc.Database.Tables[0].LogOnInfo.ConnectionInfo.UserID;
if (UserID == null) MessageBox.Show("UserID has NULL");