原創(chuàng)|使用教程|編輯:我只采一朵|2014-01-21 09:43:04.000|閱讀 1562 次
概述:本節(jié)介紹如何用eXpress Persistent Objects (XPO) 創(chuàng)建一個能自動保存數(shù)據到服務器的ASP.NET應用程序。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
上節(jié)跟大家講了如何用對象映射工具 eXpress Persistent Objects (XPO) 創(chuàng)建數(shù)據查詢條件。本節(jié)我們來創(chuàng)建一個能自動保存數(shù)據到服務器的ASP.NET應用程序,用于查看和編輯用戶詳細信息。
創(chuàng)建一個新項目后,我們首先要做的就是定義一個Persistent Object Class,將它作為Customer類,如圖:
這個類的FirstName, LastName and Company屬性代表數(shù)據庫表相應字段的值。
using DevExpress.Xpo; public class Customer : XPObject { public Customer(Session session) : base(session) {} public string FirstName; public string LastName; public string Company; }
接下來將XPO連接到數(shù)據庫服務器。方法是創(chuàng)建一個IDataLayer 對象,創(chuàng)建數(shù)據層的代碼必須放在Application_Start事件處理器中(注:這個處理器在站點的Global.asax模塊中)。
void Application_Start(object sender, EventArgs e) { // Code that runs on the application startup // Specify the connection string, which is used to open a database. // It's supposed that you've already created the Comments database within the App_Data folder. string conn = DevExpress.Xpo.DB.AccessConnectionProvider.GetConnectionString( Server.MapPath("~\\App_Data\\Customer.mdb")); DevExpress.Xpo.Metadata.XPDictionary dict = new DevExpress.Xpo.Metadata.ReflectionDictionary(); // Initialize the XPO dictionary. dict.GetDataStoreSchema(typeof(Customer).Assembly); DevExpress.Xpo.XpoDefault.Session = null; DevExpress.Xpo.DB.IDataStore store = DevExpress.Xpo.XpoDefault.GetConnectionProvider(conn, DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema); DevExpress.Xpo.XpoDefault.DataLayer = new DevExpress.Xpo.ThreadSafeDataLayer(dict, store); }
Persistent對象使用 XpoDataSource 組件檢索數(shù)據。在聲明了Customer類之后,必須重建方案,從工具箱中將XpoDataSource拖放到頁面上。然后,部署persistent類到Typename屬性:
連接XpoDataSource組件:
Session session1; protected void Page_Init(object sender, EventArgs e) { session1 = new Session(); // ... XpoDataSource1.Session = session1; }
那么如何顯示數(shù)據呢?還需要部署XpoDataSource組件。這個例子中,我們使用了ASPxGridView。下圖展示了如何綁定XpoDataSource到網格:
XpoDataSource綁定到網格后,網格會為所有的persistent屬性自動生成數(shù)據列。啟動數(shù)據編輯的方式:
最后,添加數(shù)據到Customer表格中并運行項目:
Session session1; protected void Page_Load(object sender, EventArgs e) { session1 = new Session(); if (session1.FindObject<Customer>(null) == null) { Customer cstm = new Customer(session1); cstm.FirstName = "John"; cstm.LastName = "Doe"; cstm.Company = "Doe Enterprises"; cstm.Save(); cstm = new Customer(session1); cstm.FirstName = "Sam"; cstm.LastName = "Hill"; cstm.Company = "Hill Corporation"; cstm.Save(); } XpoDataSource1.Session = session1; }
最終結果:
用戶可以查看并編輯網格中的數(shù)據,最重要的是,你無需編寫代碼去保存這些數(shù)據。一旦表格發(fā)生變化,它就會自動保存到服務器上。
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件