原創(chuàng)|使用教程|編輯:我只采一朵|2014-02-27 09:41:59.000|閱讀 1738 次
概述:本節(jié)介紹用eXpress Persistent Objects (XPO)創(chuàng)建項(xiàng)目和初始化數(shù)據(jù)連接。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
相關(guān)鏈接:
前面為大家介紹了eXpress Persistent Objects (XPO)創(chuàng)建數(shù)據(jù)識(shí)別應(yīng)用程序的基本步驟,最后還要用代碼加載一些屬性并初始化數(shù)據(jù)連接。下面為你一一道來(lái)。
創(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方法用于檢測(cè)數(shù)據(jù)庫(kù)是否包含Customers表格。默認(rèn)情況下,這個(gè)名稱和項(xiàng)目保存的名稱是一致的。如果沒(méi)有找到表格,就會(huì)創(chuàng)建一個(gè),然后將新的數(shù)據(jù)保存到數(shù)據(jù)表中。
初始化數(shù)據(jù)庫(kù)連接
初始化Data Access Layer,調(diào)用應(yīng)用程序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類會(huì)自動(dòng)通過(guò)ORM Data Model Wizard添加到項(xiàng)目中。如果你不使用向?qū)В梢允謩?dòng)部署數(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);
運(yùn)行項(xiàng)目,XPO自動(dòng)為persistent項(xiàng)目生成數(shù)據(jù)庫(kù)。如果數(shù)據(jù)庫(kù)不存在,XPO會(huì)創(chuàng)建一個(gè)。用戶可以查看和編輯網(wǎng)格中的數(shù)據(jù)。這個(gè)應(yīng)用程序還有一個(gè)優(yōu)點(diǎn)就是你不需要寫(xiě)任何代碼去保存數(shù)據(jù),所有的變化都會(huì)被自動(dòng)保存。應(yīng)用程序效果圖:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件