原創(chuàng)|使用教程|編輯:我只采一朵|2014-01-21 09:43:04.000|閱讀 1562 次
概述:本節(jié)介紹如何用eXpress Persistent Objects (XPO) 創(chuàng)建一個(gè)能自動(dòng)保存數(shù)據(jù)到服務(wù)器的ASP.NET應(yīng)用程序。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
上節(jié)跟大家講了如何用對(duì)象映射工具 eXpress Persistent Objects (XPO) 創(chuàng)建數(shù)據(jù)查詢條件。本節(jié)我們來創(chuàng)建一個(gè)能自動(dòng)保存數(shù)據(jù)到服務(wù)器的ASP.NET應(yīng)用程序,用于查看和編輯用戶詳細(xì)信息。
創(chuàng)建一個(gè)新項(xiàng)目后,我們首先要做的就是定義一個(gè)Persistent Object Class,將它作為Customer類,如圖:
這個(gè)類的FirstName, LastName and Company屬性代表數(shù)據(jù)庫表相應(yīng)字段的值。
using DevExpress.Xpo; public class Customer : XPObject { public Customer(Session session) : base(session) {} public string FirstName; public string LastName; public string Company; }
接下來將XPO連接到數(shù)據(jù)庫服務(wù)器。方法是創(chuàng)建一個(gè)IDataLayer 對(duì)象,創(chuàng)建數(shù)據(jù)層的代碼必須放在Application_Start事件處理器中(注:這個(gè)處理器在站點(diǎn)的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對(duì)象使用 XpoDataSource 組件檢索數(shù)據(jù)。在聲明了Customer類之后,必須重建方案,從工具箱中將XpoDataSource拖放到頁面上。然后,部署persistent類到Typename屬性:
連接XpoDataSource組件:
Session session1; protected void Page_Init(object sender, EventArgs e) { session1 = new Session(); // ... XpoDataSource1.Session = session1; }
那么如何顯示數(shù)據(jù)呢?還需要部署XpoDataSource組件。這個(gè)例子中,我們使用了ASPxGridView。下圖展示了如何綁定XpoDataSource到網(wǎng)格:
XpoDataSource綁定到網(wǎng)格后,網(wǎng)格會(huì)為所有的persistent屬性自動(dòng)生成數(shù)據(jù)列。啟動(dòng)數(shù)據(jù)編輯的方式:
最后,添加數(shù)據(jù)到Customer表格中并運(yùn)行項(xiàng)目:
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; }
最終結(jié)果:
用戶可以查看并編輯網(wǎng)格中的數(shù)據(jù),最重要的是,你無需編寫代碼去保存這些數(shù)據(jù)。一旦表格發(fā)生變化,它就會(huì)自動(dòng)保存到服務(wù)器上。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件