上一篇文章中只是简单的写了一个入门的示例,在这篇中把原理初步的说说以及

在绑定一个数据类的时候应该注意到的地方;

1.先创建一个实体类:

[DataServiceKey("Name")]
  public class CustomerMDL
  {
      public string Name { set; get; }
      public string Address { set; get; }
  }
注意:这里一定要指定DataServiceKey这个特性,代的作用就是,指定这个类中的数据那个是主键值
 
2.创建一个数据储藏类
public class CustomerStore
   {
       private static List<CustomerMDL> customer;

       public IQueryable<CustomerMDL> Customer
       {
           get
           {
               if (CustomerStore.customer == null)
               {
                   CustomerStore.customer = new List<CustomerMDL>();
                   CustomerStore.customer.Add(new CustomerMDL
                   {
                       Name = "cao",
                       Address = "Si Chuan"
                   });

                   CustomerStore.customer.Add(new CustomerMDL
                   {
                       Name = "good Man",
                       Address = "Si Chuan"
                   });


                   CustomerStore.customer.Add(new CustomerMDL
                   {
                       Name = "good Man",
                       Address = "Si Chuan"
                   });

               }
               return CustomerStore.customer.AsQueryable();

           }
       }
   }
 
3.创建一个ADO.NET Data Service类
public class MyCustomer : DataService<CustomerStore>
   {
       // 仅调用此方法一次以初始化涉及服务范围的策略。
       public static void InitializeService(IDataServiceConfiguration config)
       {
           // TODO: 设置规则以指明哪些实体集和服务操作是可见的、可更新的,等等。
           // 示例:
           config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
           // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
       }
   }
4.运行服务如下:
ADO.NET Data Service 二 绑定数据类 
输入Customer将显示所有数据的条数
 
ADO.NET Data Service 二 绑定数据类 
5.创建一个默认的页面,显示数据:
protected void Page_Load(object sender, EventArgs e)
      {
          DataServiceContext context = new DataServiceContext(new 
Uri("http://localhost:8202/MyCustomer.svc/"));

          this.GridView1.DataSource = context.Execute<CustomerMDL>(new Uri("Customer", UriKind.Relative));
          this.GridView1.DataBind();
      }
运行效果如下:
ADO.NET Data Service 二 绑定数据类
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

相关文章:

  • 2021-07-20
  • 2021-09-02
  • 2021-09-15
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2021-06-09
  • 2022-02-12
猜你喜欢
  • 2021-08-06
  • 2021-10-11
  • 2021-08-07
  • 2022-02-12
  • 2022-02-23
  • 2021-12-20
  • 2021-10-03
相关资源
相似解决方案