ADO.NET Data Service 二 綁定資料類

來源:互聯網
上載者:User

上一篇文章中只是簡單的寫了一個入門的樣本,在這篇中把原理初步的說說以及

在綁定一個資料類的時候應該注意到的地方;

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.運行服務如下:
 
輸入Customer將顯示所有資料的條數
 
 
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();      }

運行效果如下:

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.