将 Reporting Services 与 Silverlight 和 RIA Services 集成





5.00/5 (8投票s)
将 Reporting Services 与你的 Silverlight 业务线应用程序集成。
引言
Silverlight 中最受欢迎的未来功能之一是使用 Reporting Services 创建业务报告的可能性。 在本文中,我将展示如何将现有的 Reporting Services 技术与 Silverlight 和 RIA Services 组合一起使用。
背景
我假设读者具有 Silverlight 和 RIA Services 以及 C# 的一些基本到中级水平的知识,并且对 WCF 和 Entity Framework 也有一些了解。
Using the Code
首先要做的是在 Visual Studio 2010 中创建一个 Silverlight 应用程序。我使用了 Silverlight Business Application 模板。
在 ASP.NET 项目中,我添加了一个 Entity Framework Model。
在这个演示中,我正在使用 AdventureWorksLT 数据库。
这些是我们将在此演示中使用的实体
构建项目并添加一个 Domain Service 类
这是所有选定要添加到域服务的实体
现在我们可以添加一个新的报表。 在本例中,我们使用本地模式的 Reporting Services。
在报表中,添加 Tablix 组件时,会请求一个数据集。 在本例中,选择数据集向导会看到我们之前添加的域上下文。
对于此演示,我创建了一个新类,我将使用该类绑定到报表。 在某些情况下,这样做是可取的,因为在将数据绑定到报表之前,我们可能需要进行一些额外的处理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace ReportingServicesDemo.Web
{
public class Orders
{
public Orders()
{ }
[Key]
public int OrderDetailsID
{
get;
set;
}
public int OrderID
{
get;
set;
}
public string Product
{
get;
set;
}
public int Quantity
{
get;
set;
}
public decimal UnitPrice
{
get;
set;
}
public decimal UnitPriceDiscount
{
get;
set;
}
public decimal LineTotal
{
get;
set;
}
}
}
在这个类中,我引用了 System.ComponentModel.DataAnnotations
命名空间,这允许我向 OrderDetailsID
属性添加一个 Key
属性 - 这是该类的要求,以便域服务可以将其识别为一个实体。
如果我们深入研究域服务代码,我们会发现这个
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
//// Implements application logic using the AdventureWorksLT2008Entities context.
//// TODO: Add your application logic to these methods or in additional methods.
//// TODO: Wire up authentication (Windows/ASP.NET Forms)
/// and uncomment the following to disable anonymous access
//// Also consider adding roles to restrict access as appropriate.
// [RequiresAuthentication]
[EnableClientAccess()]
public class ADWLTDomainService :
LinqToEntitiesDomainService<AdventureWorksLT2008Entities>
{
现在我创建一个域操作,它公开我在自定义类中刚刚创建的 Orders
实体
public IQueryable<Orders> GetOrderDetails(int OrderID)
{
AdventureWorksLT2008Entities adw = new AdventureWorksLT2008Entities();
var query = from c in adw.SalesOrderDetail.Include("Product")
where
c.SalesOrderID == OrderID
select new Orders { OrderID = OrderID,
OrderDetailsID = c.SalesOrderDetailID, Product = c.Product.Name,
Quantity = c.OrderQty, UnitPrice =
c.UnitPrice, UnitPriceDiscount = c.UnitPriceDiscount,
LineTotal = c.LineTotal };
return query;
}
这里有一些说明。 在此方法中,我使用 Entity Framework 上下文而不是域服务声明的对象上下文,并像其他选择方法一样公开为 IQueryable
。
如果我们再次构建项目,“选择数据集向导”会看到我们的自定义方法以及实体公开的所有字段,以供我们的报表使用。
这是报表设计以及可用的数据集
现在我们向 Web 项目添加一个 WebForm,以便使用 ReportViewer
控件放置报表
当我们添加 ReportViewer
控件时,它会创建一个 ObjectDataSource
控件。
查看 ObjectDataSource
的属性,我们可以看到自定义方法的 Select 属性。
查看 SelectParameters
集合,我们看到了我们的自定义方法参数。
现在在 Silverlight 项目中,我们添加一个绑定到 Sales Orders 实体的 DataGrid
。
Sales Order ID 列中的每个字段都表示为一个按钮,仅用于此演示,以便在单击事件上传递内容值作为参数。 其想法是使用 HtmlPage
对象调用一个弹出窗口,其中包含来自 System.Windows.Browser
命名空间的 HtmlPopup
选项。
HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
options.Left = 0;
options.Top = 0;
options.Width = 930;
options.Height = 800;
options.Menubar = false;
options.Toolbar = false;
options.Directories = false;
options.Status = false;
Button btn = sender as Button;
int OrderID =int.Parse(btn.Content.ToString());
string address = Application.Current.Host.Source.AbsoluteUri;
int i = address.IndexOf("/ClientBin/", 1);
string url = address.Substring(0, i);
url = url + "/RpDemoWebForm.aspx?OrderID=" + OrderID;
if (true == HtmlPage.IsPopupWindowAllowed)
HtmlPage.PopupWindow(new Uri(url), "new", options);
我们可以使用 Application.Current.Host.Source.AbsoluteUri
属性获取 Silverlight XAP 应用程序正在运行的当前 Web 地址。 稍后,我们可以创建与 ReportViewer 的 Web Form 关联的 URI。
最后,我们可以看到我们的 Order Details 报告。
关注点
我希望这篇文章在我们等待下一个 Silverlight 版本的同时有所帮助,并且还可以探索业务报告中的更多选项。
历史
- 2010-07-26:提交给 CodeProject。