How get the data from database with xpo (devexpress)?

Zoners picture Zoners · Sep 28, 2012 · Viewed 16.1k times · Source

I'm a newbie with devexpress and i have a few difficulty.

I have create a project ".net empty c#" with visual studio. I would like connect at my database "mysql" with "devexpress xpo".
I create a "dxperience orm data model wizard" to connect at my database. At the end i have a "connectionHelper.cs"(with few method static) and a class with the name of my table.

But I not understand , how connect,read,write,... at the database with the connectionHelper? I read the documentation of devexpress but I not have the same result.

thank you in advance

The class connection helper:

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);
        }
    }
}

The class authentification.cs (authentification is the name of my table into my database)

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(); }
    }
}

The class 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); }
        }
    }
}

Answer

safi eddine picture safi eddine · Apr 5, 2017

to connect to your Datebase:

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

to read:

//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));

else you can read a Collection:

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

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

to delete:

//after getting record from database (above) 

record.Delete();

to update

//Update

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