天天看点

再接再厉VS 2008 sp1 + .NET 3.5 sp1(7) - Data Services(数据服务)

<a href="http://webabcd.blog.51cto.com/1787395/341154" target="_blank">[索引页]</a>

<a href="http://down.51cto.com/data/99915" target="_blank">[源码下载]</a>

再接再厉VS 2008 sp1 + .NET 3.5 sp1(7) - Data Services(数据服务)

介绍

以Northwind为示例数据库,演示ADO.NET Data Services(数据服务)

DataService&lt;T&gt; - ADO.NET 数据服务的主入口点。 T 为数据源类名

IDataServiceConfiguration.SetEntitySetAccessRule(string name, EntitySetRights rights) - 为指定实体集设置访问规则

QueryInterceptorAttribute - 声明在方法上的查询拦截器

DataServiceContext - 数据服务的上下文

DataServiceQuery - 以指定的 URI 语法查询数据服务

示例

服务端

Service.cs

using System; 

using System.Data.Services; 

using System.Collections.Generic; 

using System.Linq; 

using System.ServiceModel.Web; 

using VS2008SP1.Business; 

using System.Linq.Expressions; 

// DataService&lt;T&gt; - ADO.NET 数据服务的主入口点。 T 为数据源类名 

public class Service : DataService&lt;VS2008SP1.Business.NorthwindEntities&gt; 

        // 初始化涉及服务范围的策略(仅调用一次) 

static void InitializeService() static void InitializeService(IDataServiceConfiguration config) 

        { 

                /**//* 

                 * IDataServiceConfiguration.SetEntitySetAccessRule(string name, EntitySetRights rights) - 为指定实体集设置访问规则 

                 *         name - 实体集名称(* 为全部) 

                 *         rights - 设置对指定实体集的操作权限 [System.Data.Services.EntitySetRights 枚举] 

                 * EntitySetRights.None - 权限为:无任何权限     

                 * EntitySetRights.All - 权限为:全部权限     

                 * EntitySetRights.ReadSingle - 权限为:只能读取数据集内的某一条数据 

                 * EntitySetRights.ReadMultiple - 权限为:可读取数据集内的全部数据 

                 * EntitySetRights.WriteAppend - 权限为:插入     

                 * EntitySetRights.WriteReplace - 权限为:更新     

                 * EntitySetRights.WriteDelete - 权限为:删除 

                 * EntitySetRights.WriteMerge - 权限为:合并数据     

                 * EntitySetRights.AllRead - 权限为:全部可读     

                 * EntitySetRights.AllWrite - 权限为:全部可写     

                 */ 

                config.SetEntitySetAccessRule("*", EntitySetRights.All); 

                // config.SetEntitySetAccessRule("*", EntitySetRights.AllRead | EntitySetRights.AllWrite); 

        } 

        /**//* 

         *    QueryInterceptorAttribute - 声明在方法上的查询拦截器 

         *            方法名任意(不带参数),返回值必须为一个 Expression&lt;Func&lt;T, bool&gt;&gt; 表达式,T 为需要过滤的实体集类名 

         */ 

        [QueryInterceptor("Orders")] 

Expression&lt;Func&lt;Orders, bool&gt;&gt; FilterOrders() Expression&lt;Func&lt;Orders, bool&gt;&gt; FilterOrders() 

                // 本例为无论在任何情况下,返回的 Orders 都是 ContactName 为 Paul Henriot 的 Orders 

                return o =&gt; o.Customers.ContactName == "Paul Henriot"; 

}

1、添加、查询、更新和删除的Demo

Demo.aspx

&lt;%@ Page Title="添加、查询、更新和删除的Demo" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 

        CodeFile="Demo.aspx.cs" Inherits="DataServices_Demo" %&gt; 

&lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"&gt; 

&lt;/asp:Content&gt; 

&lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"&gt; 

        &lt;div style="margin: 10px;"&gt; 

                CategoryName:&lt;asp:TextBox ID="txtCategoryName" runat="server" /&gt; 

                Description:&lt;asp:TextBox ID="txtDescription" runat="server" /&gt; 

                &lt;asp:Button ID="btnAddCategory" runat="server" Text="添加产品类别" OnClick="btnAddCategory_Click" /&gt; 

        &lt;/div&gt; 

                &lt;asp:GridView ID="GridView1" DataKeyNames="CategoryID" runat="server" AutoGenerateColumns="false" 

                        OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" 

                        OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"&gt; 

                        &lt;Columns&gt; 

                                &lt;asp:BoundField DataField="CategoryID" HeaderText="ID" ReadOnly="true" /&gt; 

                                &lt;asp:BoundField DataField="CategoryName" HeaderText="CategoryName" /&gt; 

                                &lt;asp:BoundField DataField="Description" HeaderText="Description" /&gt; 

                                &lt;asp:CommandField ShowSelectButton="True" ShowEditButton="True" ShowDeleteButton="True"&gt; 

                                &lt;/asp:CommandField&gt; 

                        &lt;/Columns&gt; 

                &lt;/asp:GridView&gt; 

                &lt;asp:GridView ID="GridView2" runat="server"&gt; 

&lt;/asp:Content&gt;

Demo.aspx.cs

// 客户端用到的只有以下两个类 

// System.Data.Services.Client.DataServiceContext - 数据服务的上下文 

// System.Data.Services.Client.DataServiceQuery - 以指定的 URI 语法查询数据服务 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls; 

using MyDataService; 

public partial class DataServices_Demo : System.Web.UI.Page 

        // ADO.NET 数据服务的地址 

        Uri _uri = new Uri("http://localhost:7002/DataServices/Service.svc/", UriKind.Absolute); 

void Page_Load() void Page_Load(object sender, EventArgs e) 

                if (!Page.IsPostBack) 

                { 

                        BindData(); 

                } 

        void BindData() 

                // NorthwindEntities - 继承自 DataServiceContext ,其构造函数的参数为数据服务的 URI 地址 

                NorthwindEntities ctx = new NorthwindEntities(_uri); 

                // 将 System.Data.Services.Client.DataServiceQuery&lt;Categories&gt; 绑定到 Gridview1 

                GridView1.DataSource = ctx.Categories; 

                GridView1.DataBind(); 

void btnAddCategory_Click() void btnAddCategory_Click(object sender, EventArgs e) 

                Categories category = new Categories(); 

                category.CategoryName = txtCategoryName.Text; 

                category.Description = txtDescription.Text; 

                // DataServiceContext.Timeout - 超时时间 

                // DataServiceContext.MergeOption - 当前数据上下文与数据服务的数据的同步选项 [System.Data.Services.Client.MergeOption 枚举] 

                //         AppendOnly - 追加。默认值 

                //         OverwriteChanges - 以新结果为准 

                //         PreserveChanges - 以原结果为准 

                //         NoTracking - 取消变更跟踪,不会使用 ObjectStateManager,减少执行查询的时间,所有返回的实体将是分离的状态(detached state) 

                ctx.MergeOption = System.Data.Services.Client.MergeOption.AppendOnly; 

                ctx.AddToCategories(category); 

                for (int i = 0; i &lt; 10; i++) 

                        var product = new Products() { ProductName = "测试用" + i.ToString() }; 

                        product.Categories = category; 

                        ctx.AddToProducts(product); 

                        // DataServiceContext.SetLink(Object source, string sourceProperty, Object target) - 在指定对象上创建新的连接,Added状态,SaveChanges() 的时候会一起发送到数据服务。多对一关系 

                        //         source - 源对象 

                        //         sourceProperty - 源对象上的属性,用于标识目标对象的类型 

                        //         target - 目标对象 

                        ctx.SetLink(product, "Categories", category); 

                        // DataServiceContext.AddLink(Object source, string sourceProperty, Object target) - 在指定对象上创建新的连接,Added状态,SaveChanges() 的时候会一起发送到数据服务。一对多关系 

                        // DataServiceContext.DeleteLink(Object source, string sourceProperty, Object target) - 在指定的对象上删除连接,Deleted状态,SaveChanges() 的时候会一起发送到数据服务 

                // DataServiceContext.SaveChanges() - 将所有更新保存到相关存储区中 

                //         SaveChangesOptions.Batch - 一个 http 提交所有操作 

                //         SaveChangesOptions.ContinueOnError - 一个 http 一个操作,某操作出错仍将处理后续操作(不能与 Batch 共存) 

                //         SaveChangesOptions.ReplaceOnUpdate -    

                //         SaveChangesOptions.None - 出错则停止 

                ctx.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch); 

                Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "alert('CategoryID: " + category.CategoryID + "');", true); 

                GridView1.EditIndex = -1; 

                BindData(); 

void GridView1_RowDeleting() void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 

                var categoryId = (int)GridView1.DataKeys[e.RowIndex].Value; 

                var category = ctx.Categories.Where(p =&gt; p.CategoryID == categoryId).First(); 

                // DataServiceContext.LoadProperty(Object entity, string propertyName) - 加载指定对象的指定实体属性 

                //         entity - 包含要加载的实体属性的实体 

                //         propertyName - 需要加载的实体属性的名称 

                ctx.LoadProperty(category, "Products"); 

                foreach (var product in category.Products) 

                        ctx.DeleteObject(product); 

                ctx.DeleteObject(category); 

                ctx.SaveChanges(); 

void GridView1_SelectedIndexChanged() void GridView1_SelectedIndexChanged(object sender, EventArgs e) 

                var categoryId = (int)GridView1.SelectedDataKey.Value; 

                // 查找当前选中的类别下的产品集合 

                var products = ctx.Products.Where(p =&gt; p.Categories.CategoryID == categoryId); 

                // 将 System.Linq.IQueryable&lt;Products&gt; 绑定到 GridView2 

                GridView2.DataSource = products; 

                GridView2.DataBind(); 

void GridView1_RowUpdating() void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 

                category.CategoryName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text; 

                category.Description = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 

                ctx.UpdateObject(category); 

void GridView1_RowEditing() void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 

                GridView1.EditIndex = e.NewEditIndex; 

void GridView1_RowCancelingEdit() void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 

2、其他知识点

Demo2.aspx

&lt;%@ Page Title="其它" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 

        CodeFile="Demo2.aspx.cs" Inherits="DataServices_Demo2" %&gt; 

        &lt;div id="result" runat="server"&gt; 

Demo2.aspx.cs

using System.Data.Services.Client; 

public partial class DataServices_Demo2 : System.Web.UI.Page 

                // 以 URI 语法的方式查询 ADO.NET 数据服务 

                Demo(); 

                // 异步方式查询 ADO.NET 数据服务 

                Demo2(); 

                // 演示查询拦截器的 Demo 

                Demo3(); 

                // 演示以 JSON 作为响应格式的 Demo 

                Demo4(); 

        void Demo() 

                DataServiceContext ctx = new DataServiceContext(_uri); 

                // 以 URI 语法的方式查询 ADO.NET 数据服务,详细语法参看 MSDN 

                // http://[Url]/[ServiceName]/[EntityName]/[NavigationOptions]?[QueryOptions] 

                DataServiceQuery&lt;Categories&gt; categories = ctx.CreateQuery&lt;Categories&gt;("/Categories"); 

                foreach (var c in categories) 

                        result.InnerHtml += c.CategoryID + ","; 

                result.InnerHtml += "&lt;br /&gt;&lt;br /&gt;"; 

        void Demo2() 

                DataServiceQuery&lt;Categories&gt; query = ctx.CreateQuery&lt;Categories&gt;("/Categories"); 

                query.BeginExecute( 

                        delegate(IAsyncResult ar) 

                        { 

                                foreach (var c in query.EndExecute(ar)) 

                                { 

                                        result.InnerHtml += c.CategoryID + ","; 

                                } 

                                result.InnerHtml += "&lt;br /&gt;&lt;br /&gt;"; 

                        }, 

                        null); 

        void Demo3() 

                // 因为设置了查询拦截器,所以只能收到 ContactName 为 Paul Henriot 的 Orders 

                foreach (var o in ctx.Orders) 

                        result.InnerHtml += o.OrderID + ","; 

        void Demo4() 

                System.Net.WebClient client = new System.Net.WebClient(); 

                // 需要以 json 格式返回结果的话,需要设置以下 header 

                client.Headers.Add("accept", "application/json"); 

                // client.Headers.Add("accept", "application/xml"); xml/atom 格式 

                // client.Headers.Add("accept", "*/*"); xml/atom 格式,默认值 

                result.InnerHtml += HttpUtility.HtmlEncode(client.DownloadString(_uri)); 

OK

      本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/341567,如需转载请自行联系原作者

继续阅读