簡體   English   中英

在ASP.NET MVC中導出具有多個表的Crystal Reports

[英]Export Crystal Reports with multiple tables in ASP.NET MVC

我正在嘗試從Web應用程序打印訂單報告。 我必須連接3個表才能獲取所需的數據。

例如,我有一個OrdersContractProducts表。 我還有另一個表ContractProducts ,這是合同和產品的過渡表。 訂單表具有作為外鍵的客戶ID,合同表也具有針對客戶ID的外鍵。

我實際上也有一個Customers表,但是我決定只使用訂單表中的客戶ID,因為我沒有使用客戶表。 長話短說,我需要顯示訂單ID,合同ID和與訂單關聯的產品。 我有以下代碼,它給我一個錯誤,指出檢索數據庫時出錯。 我使用了join方法,但是它仍然無法正常工作。 請幫我。 我已經花了幾個小時了:(

//此功能用於導出訂單報告

public ActionResult CreateOrderReport()
    {
        try
        {              

            var orders = (from o in db.Orders
                     join con in db.Contracts on o.CustomerID equals con.CustomerID
                     join prod in db.ContractProducts on con.ContractID equals prod.ContractID
                     select new
                     {
                         OrderID = o.OrderID.ToString(),
                         CustomerID = o.Customer.CustomerID.ToString(),
                         CustomerName = o.Customer.CustomerName.ToString(),
                         Phone = o.Customer.Phone.ToString(),
                         ProductID = prod.ProductID.ToString(),
                         CategoryID = prod.Product.CategoryID.ToString(),
                         Brand = prod.Product.Brand.ToString(),
                         Volume = prod.Product.Volume.ToString(),
                         PackSize = prod.Product.PackSize.ToString(),
                         Quantity = prod.Quantity.ToString()
                     }).ToList();


            ReportDocument Rep = new ReportDocument();

            Rep.Load(Path.Combine(Server.MapPath("~/Reports/OrderReport.rpt")));

            Rep.SetDataSource(orders);
            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();

            Stream stream = Rep.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            stream.Seek(0, SeekOrigin.Begin);
            return File(stream, "application/pdf", "Orders Report.pdf");
        }
        catch (DataException ex)
        {
            throw;
        }

    }
***Replace this part***

********************************************************************

    var orders = (from o in db.Orders
    join con in db.Contracts on o.CustomerID equals con.CustomerID
    join prod in db.ContractProducts on con.ContractID equals prod.ContractID
    select new
    {
     OrderID = o.OrderID.ToString(),
     CustomerID = o.Customer.CustomerID.ToString(),
     CustomerName = o.Customer.CustomerName.ToString(),
    Phone = o.Customer.Phone.ToString(),
     ProductID = prod.ProductID.ToString(),
     CategoryID = prod.Product.CategoryID.ToString(),
     Brand = prod.Product.Brand.ToString(),
     Volume = prod.Product.Volume.ToString(),
     PackSize = prod.Product.PackSize.ToString(),
     Quantity = prod.Quantity.ToString()
    }).ToList();

********************************************************************
***with this***
********************************************************************
db.Orders.Select(p => new
{
    OrderID = o.OrderID.ToString(),
     CustomerID = o.Customer.CustomerID.ToString(),
     CustomerName = o.Customer.CustomerName.ToString(),
     Phone = o.Customer.Phone.ToString(),
     ProductID = prod.ProductID.ToString(),
     CategoryID = prod.Product.CategoryID.ToString(),
     Brand = prod.Product.Brand.ToString(),
     Volume = prod.Product.Volume.ToString(),
     PackSize = prod.Product.PackSize.ToString(),
     Quantity = prod.Quantity.ToString()
}).ToList());
********************************************************************
***and create Dataset with DataTable name is **

> Orderexport

** have this column :***

    OrderID 
    CustomerID 
    CustomerName 
    Phone 
    ProductID 
    CategoryID 
    Brand
    Volume 
    PackSize 
    Quantity


***And change the database source of your Crystal Reports "OrderReport.rpt"***


***

> It works well

***

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM