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

[索引页]
[源码下载]


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


作者: webabcd


介绍
以Northwind为示例数据库,演示ADO.NET Data Services(数据服务)
  • DataService<T> - 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<T> - ADO.NET 数据服务的主入口点。 T 为数据源类名
public   class  Service : DataService < VS2008SP1.Business.NorthwindEntities >
{
    
// 初始化涉及服务范围的策略(仅调用一次)
    public 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<Func<T, bool>> 表达式,T 为需要过滤的实体集类名
     
*/

    [QueryInterceptor(
"Orders")]
    
public Expression<Func<Orders, bool>> FilterOrders()
    
{
        
// 本例为无论在任何情况下,返回的 Orders 都是 ContactName 为 Paul Henriot 的 Orders
        return o => o.Customers.ContactName == "Paul Henriot";
    }

}


1、添加、查询、更新和删除的Demo
Demo.aspx
<% @ Page Title="添加、查询、更新和删除的Demo" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile
="Demo.aspx.cs" Inherits="DataServices_Demo" 
%>

< asp:Content  ID ="Content1"  ContentPlaceHolderID ="head"  runat ="Server" >
</ asp:Content >
< asp:Content  ID ="Content2"  ContentPlaceHolderID ="ContentPlaceHolder1"  runat ="Server" >
    
< div  style ="margin: 10px;" >
        CategoryName:
< asp:TextBox  ID ="txtCategoryName"  runat ="server"   />
        Description:
< asp:TextBox  ID ="txtDescription"  runat ="server"   />
        
< asp:Button  ID ="btnAddCategory"  runat ="server"  Text ="添加产品类别"  OnClick ="btnAddCategory_Click"   />
    
</ div >
    
< div  style ="margin: 10px;" >
        
< 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" >
            
< Columns >
                
< asp:BoundField  DataField ="CategoryID"  HeaderText ="ID"  ReadOnly ="true"   />
                
< asp:BoundField  DataField ="CategoryName"  HeaderText ="CategoryName"   />
                
< asp:BoundField  DataField ="Description"  HeaderText ="Description"   />
                
< asp:CommandField  ShowSelectButton ="True"  ShowEditButton ="True"  ShowDeleteButton ="True" >
                
</ asp:CommandField >
            
</ Columns >
        
</ asp:GridView >
    
</ div >
    
< div  style ="margin: 10px;" >
        
< asp:GridView  ID ="GridView2"  runat ="server" >
        
</ asp:GridView >
    
</ div >
</ asp:Content >

Demo.aspx.cs
//  客户端用到的只有以下两个类
//  System.Data.Services.Client.DataServiceContext - 数据服务的上下文
//  System.Data.Services.Client.DataServiceQuery - 以指定的 URI 语法查询数据服务

using  System;
using  System.Collections.Generic;
using  System.Linq;
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);

    
protected 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<Categories> 绑定到 Gridview1
        GridView1.DataSource = ctx.Categories;
        GridView1.DataBind();
    }


    
protected void btnAddCategory_Click(object sender, EventArgs e)
    
{
        NorthwindEntities ctx 
= new NorthwindEntities(_uri);

        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 < 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();
    }


    
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        NorthwindEntities ctx 
= new NorthwindEntities(_uri);

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

        var category 
= ctx.Categories.Where(p => 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();

        GridView1.EditIndex 
= -1;
        BindData();
    }


    
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    
{
        NorthwindEntities ctx 
= new NorthwindEntities(_uri);

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

        
// 查找当前选中的类别下的产品集合
        var products = ctx.Products.Where(p => p.Categories.CategoryID == categoryId);

        
// 将 System.Linq.IQueryable<Products> 绑定到 GridView2
        GridView2.DataSource = products;
        GridView2.DataBind();
    }


    
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        NorthwindEntities ctx 
= new NorthwindEntities(_uri);

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

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

        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);
        ctx.SaveChanges();

        GridView1.EditIndex 
= -1;
        BindData();
    }


    
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        BindData();
    }


    
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        BindData();
    }

}



2、其他知识点
Demo2.aspx
<% @ Page Title="其它" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile
="Demo2.aspx.cs" Inherits="DataServices_Demo2" 
%>

< asp:Content  ID ="Content1"  ContentPlaceHolderID ="head"  runat ="Server" >
</ asp:Content >
< asp:Content  ID ="Content2"  ContentPlaceHolderID ="ContentPlaceHolder1"  runat ="Server" >
    
< div  id ="result"  runat ="server" >
    
</ div >
</ asp:Content >

Demo2.aspx.cs
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;

using  MyDataService;
using  System.Data.Services.Client;

public   partial   class  DataServices_Demo2 : System.Web.UI.Page
{
    
// ADO.NET 数据服务的地址
    Uri _uri = new Uri("http://localhost:7002/DataServices/Service.svc/", UriKind.Absolute);

    
protected void Page_Load(object sender, EventArgs e)
    
{
        
// 以 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<Categories> categories = ctx.CreateQuery<Categories>("/Categories");

        
foreach (var c in categories)
        
{
            result.InnerHtml 
+= c.CategoryID + ",";
        }


        result.InnerHtml 
+= "<br /><br />";
    }


    
void Demo2()
    
{
        DataServiceContext ctx 
= new DataServiceContext(_uri);

        DataServiceQuery
<Categories> query = ctx.CreateQuery<Categories>("/Categories");

        
// 异步方式查询 ADO.NET 数据服务
        query.BeginExecute(
            
delegate(IAsyncResult ar)
            
{
                
foreach (var c in query.EndExecute(ar))
                
{
                    result.InnerHtml 
+= c.CategoryID + ",";
                }


                result.InnerHtml 
+= "<br /><br />";
            }
,
            
null);
    }


    
void Demo3()
    
{
        NorthwindEntities ctx 
= new NorthwindEntities(_uri);

        
// 因为设置了查询拦截器,所以只能收到 ContactName 为 Paul Henriot 的 Orders
        foreach (var o in ctx.Orders)
        
{
            result.InnerHtml 
+= o.OrderID + ",";
        }


        result.InnerHtml 
+= "<br /><br />";
    }


    
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
[源码下载]

你可能感兴趣的:(service)