原創(chuàng)|使用教程|編輯:我只采一朵|2014-02-27 09:41:59.000|閱讀 1738 次
概述:本節(jié)介紹用eXpress Persistent Objects (XPO)創(chuàng)建項目和初始化數(shù)據(jù)連接。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
前面為大家介紹了eXpress Persistent Objects (XPO)創(chuàng)建數(shù)據(jù)識別應用程序的基本步驟,最后還要用代碼加載一些屬性并初始化數(shù)據(jù)連接。下面為你一一道來。
創(chuàng)建并保存Objects
處理表單的Load事件并將記錄添加到Customers表格,填充XPCollection:
private void XtraForm1_Load(object sender, EventArgs e) { if (xpCollection1.Count == 0) { var customer1 = new Customer(session1); customer1.Name = "John"; customer1.Age = 21; customer1.Save(); xpCollection1.Add(customer1); var customer2 = new Customer(session1); customer2.Name = "Bob"; customer2.Age = 37; customer2.Save(); xpCollection1.Add(customer2); } }
XPBaseObject.Save方法用于檢測數(shù)據(jù)庫是否包含Customers表格。默認情況下,這個名稱和項目保存的名稱是一致的。如果沒有找到表格,就會創(chuàng)建一個,然后將新的數(shù)據(jù)保存到數(shù)據(jù)表中。
初始化數(shù)據(jù)庫連接
初始化Data Access Layer,調(diào)用應用程序Main方法下的 ConnectionHelper.Connect :
using DevExpress.Xpo.DB; // ... [STAThread] static void Main() { ConnectionHelper.Connect(AutoCreateOption.DatabaseAndSchema); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); DevExpress.Skins.SkinManager.EnableFormSkins(); Application.Run(new XtraForm1()); }
ConnectionHelper類會自動通過ORM Data Model Wizard添加到項目中。如果你不使用向?qū)В梢允謩硬渴?數(shù)據(jù)層到靜態(tài)的XpoDefault.DataLayer屬性,而不用調(diào)用ConnectionHelper.Connect。
using DevExpress.Xpo; using DevExpress.Xpo.DB; // ... public const string ConnectionString = @"XpoProvider=MSSqlServer;data source=(local);integrated security=SSPI;initial catalog=XpoTutorial1"; XpoDefault.DataLayer = XpoDefault.GetDataLayer(ConnectionString, AutoCreateOption.DatabaseAndSchema);
運行項目,XPO自動為persistent項目生成數(shù)據(jù)庫。如果數(shù)據(jù)庫不存在,XPO會創(chuàng)建一個。用戶可以查看和編輯網(wǎng)格中的數(shù)據(jù)。這個應用程序還有一個優(yōu)點就是你不需要寫任何代碼去保存數(shù)據(jù),所有的變化都會被自動保存。應用程序效果圖:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件