如何从xpo(devexpress)获取数据库中的数据?

时间:2012-09-28 15:10:21

标签: c# asp.net devexpress xpo

我是devexpress的新手,我有一些困难。

我用visual studio创建了一个“.net empty c#”项目。我想在我的数据库“mysql”和“devexpress xpo”连接 我创建了一个“dxperience orm数据模型向导”来连接我的数据库。 最后,我有一个“connectionHelper.cs”(几个静态方法)和一个名为my table的类。

但我不明白,连接,读,写,如何在数据库中使用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(authentification是我的数据库中的表名)

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

2 个答案:

答案 0 :(得分:0)

您可以开始使用How to use XPO in an ASP.NET (Web) application知识库文章中列出的说明。

“示例”部分中提供的示例说明了实际应用的主要方案。

答案 1 :(得分: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));

你可以阅读收藏:

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