2012-09-28 39 views
0

我是一個devexpress新手,我有一些困難。如何從xpo(devexpress)獲取數據庫中的數據?

我用Visual Studio創建了一個項目「.net empty c#」。我想連接我的數據庫「mysql」與「devexpress xpo」。
我創建了一個「dxperience orm數據模型嚮導」來連接我的數據庫。 最後,我有一個「connectionHelper.cs」(幾個靜態方法)和一個類與我的表的名稱。

但我不明白,如何連接,讀,寫,...在數據庫與connectionHelper? 我讀了devexpress的文檔,但我沒有得到相同的結果。

預先感謝您

類連接助手:

using DevExpress.Xpo; 
using DevExpress.Data.Filtering; 
namespace ProduWebEmpty.produweb 
{ 
    public static class ConnectionHelper 
    { 
     public const string ConnectionString = @"XpoProvider=MySql;server=localhost;user id=root; password=; database=web;persist security info=true;CharSet=utf8;"; 
     public static void Connect(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption) 
     { 
      XpoDefault.DataLayer = XpoDefault.GetDataLayer(ConnectionString, autoCreateOption); 
      XpoDefault.Session = null; 
     } 
     public static DevExpress.Xpo.DB.IDataStore GetConnectionProvider(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption) 
     { 
      return XpoDefault.GetConnectionProvider(ConnectionString, autoCreateOption); 
     } 
     public static DevExpress.Xpo.DB.IDataStore GetConnectionProvider(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption, out IDisposable[] objectsToDisposeOnDisconnect) 
     { 
      return XpoDefault.GetConnectionProvider(ConnectionString, autoCreateOption, out objectsToDisposeOnDisconnect); 
     } 
     public static IDataLayer GetDataLayer(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption) 
     { 
      return XpoDefault.GetDataLayer(ConnectionString, autoCreateOption); 
     } 
    } 
} 

類authentification.cs(認證是我的表的名稱到我的數據庫)

using DevExpress.Xpo; 
using DevExpress.Data.Filtering; 
namespace ProduWebEmpty.produweb 
{ 
    public partial class authentification 
    { 
     public authentification(Session session) : base(session) { } 
     public authentification() : base(Session.DefaultSession) { } 
     public override void AfterConstruction() { base.AfterConstruction(); } 
    } 
} 

類authentification.designer.cs:

using DevExpress.Xpo; 
using DevExpress.Data.Filtering; 
namespace ProduWebEmpty.produweb 
{ 

    public partial class authentification : XPLiteObject 
    { 
     int fId; 
     [Key(true)] 
     public int Id 
     { 
      get { return fId; } 
      set { SetPropertyValue<int>("Id", ref fId, value); } 
     } 
     string fPseudo; 
     [Size(255)] 
     public string Pseudo 
     { 
      get { return fPseudo; } 
      set { SetPropertyValue<string>("Pseudo", ref fPseudo, value); } 
     } 
     string fMotDePasse; 
     [Size(255)] 
     public string MotDePasse 
     { 
      get { return fMotDePasse; } 
      set { SetPropertyValue<string>("MotDePasse", ref fMotDePasse, value); } 
     } 
     string fEmail; 
     [Size(255)] 
     public string Email 
     { 
      get { return fEmail; } 
      set { SetPropertyValue<string>("Email", ref fEmail, value); } 
     } 
    } 
} 

回答

0

您可以開始使用How to use XPO in an ASP.NET (Web) application知識庫文章中列出的說明。

「示例」部分提供的示例說明了主要場景的實際應用。

+0

的ConnectionHelper類只是提供了有用的方法來配置你的數據庫連接。例如,如果您使用的是ASP.NET應用程序,請將ConnectionHelper.Connect調用放入Global.asax.cs文件中的Application_Start方法中。有關更多詳細信息,請參閱上面Mike提供的幫助文章。如果您需要此任務的更多幫助,請使用官方DevExpress支持頻道:www.devexpress.com/sc(在30天評估期內免費)。 –

0

連接到你的數據庫建設:

IDataLayer db_layer = XpoDefault.GetDataLayer(new SqlConnection(@sqlCon_str), 
    DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema); 
    Session session =new Session(db_layer); 

mypersistent record =new mypersistent(session){ 
property1="string property", 
property2=123456 
}; 
record.Save(); // save new record(line) to Database With Unique Oid 

閱讀:

//if you know the Unique_Oid so: 
mypersistent record=session.FindObject<mypersistent>(CriteriaOperator.Parse("Oid = ?",Unique_Oid); 

Console.WriteLine(string.Format("property1 = {0} , property2={1}",record.property1,record.property2)); 

其他你能讀一個Collection:

XpCursor collection=new XpCursor(session,typeof(mypersistent),CriteriaOperator.Parse("property1 = '?'","string property"); 

foreach(mypersistent p in collection) { /* .... */ } 

刪除:

//after getting record from database (above) 

record.Delete(); 

更新

//Update 

record.property1="Hello"; 
record.Save();