【问题标题】:How to run crystal report on client machine without giving full path如何在客户端机器上运行水晶报表而不给出完整路径
【发布时间】:2021-01-26 08:40:50
【问题描述】:

我使用 c# 和 SQL Server Management Studio 创建了库存管理系统。在我的系统中有水晶报表。当我在客户端机器上运行我的项目时。它的工作符合预期。但我无法打开水晶报表。原因是给定路径与客户端机器路径不同。

reportDocument.Load(@"E:\Projects\WCC-StockManagementSystem\WCC-StockManagementSystem\Reports\InvoiceReport.rpt");

我想在不提供完整路径的情况下运行我的水晶报表。我想像这样改变上面的行。

 reportDocument.Load(@"WCC-StockManagementSystem\WCC-StockManagementSystem\Reports\InvoiceReport.rpt");

但如果我这样做。它不工作。你能告诉我如何解决这个问题。这是我加载水晶报表的完整代码。

reportDocument.Load(@"E:\Projects\WCC-StockManagementSystem\WCC-StockManagementSystem\Reports\InvoiceReport.rpt");
        using (SqlConnection con = new SqlConnection(CS))
        {
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select MAX(InvoiceNo) InvoiceNo, MAX(Cashier) Cashier, ItemName, Sum(Quantity) Quantity, ItemCode, MAX(DiscountPrice) DiscountPrice, MAX(Amount) Amount, MAX(GrossAmount) GrossAmount, MAX(Cash) Cash, MAX(Balance) Balance, MAX(NoOfItems) NoOfItems from tblTemporaryInvoice group by ItemCode, ItemName ", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            reportDocument.SetDataSource(dt);
            crvInvoiceReportViewer.ReportSource = reportDocument;
        }

请更改此代码并将正确的方法发送给我以在客户端计算机上运行水晶报告。谢谢

【问题讨论】:

  • 这个问题与水晶报表没有太大关系,但与您如何分发应用程序以及报表文件文件所在的位置有很大关系。例如,检查this answer
  • 每个应用程序都必须配置成功执行所需的信息。您应该使用您打算存储报告的适当目录对其进行配置,就像您对数据库的连接信息所做的那样。

标签: c# sql-server crystal-reports sql-server-2017-express


【解决方案1】:

如果不想在配置文件中添加Reports文件夹的路径,那么可以将Reports文件夹放在.exe所在的同一个路径中。

然后您可以使用Environment.CurrentDirectory 构建查找报告的路径

reportDocument.Load($"{Environment.CurrentDirectory}\Reports\InvoiceReport.rpt");

【讨论】:

  • 我无法键入 \Reports\InvoiceReport.rtp。它在 \ 标记下显示红线。
  • 你应该用另一个\“逃脱”。像 reportDocument.Load($"{Environment.CurrentDirectory}\\Reports\\InvoiceReport.rpt");
  • 您也可以使用:$"{Application.StartupPath}\\Reports\\InvoiceReport.rpt"
  • 它也不起作用。当我在客户端机器上安装我的项目并尝试打开我的水晶报表时。它显示了这个错误。加载报告失败。我用了这条线。它在我的电脑上工作。但不是客户端机器。 reportDocument.Load(Application.StartupPath.Replace("bin\\Debug", "") + "\\Reports\\InvoiceReport.rpt");
  • 您不应该使用 Replace("bin\\Debug", "")。如果您的 PC 和客户端 PC 中的 Reports 文件夹与 .exe 位于同一文件夹中,则无需进行替换。
【解决方案2】:

您无需提供完整路径。只需创建一个水晶报表的实例。

InvoiceReport rpt = new InvoiceReport();
crvInvoiceReportViewer.ReportSource = rpt;

它会起作用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    相关资源
    最近更新 更多