开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 15794|回复: 0

XPO 获取数据库服务器时间

[复制链接]

0

精华

379

贡献

793

赞扬

帖子
367
软币
7240
在线时间
785 小时
注册时间
2013-6-8
发表于 2016-5-8 12:03:16 | 显示全部楼层 |阅读模式

在开始学习XPO时,需要连接到数据库且需要获得数据库服务器时间,连接ORACLE
Session.DefaultSession.Connection = new OracleConnection("Data Source=dbserver;User ID=system;Password=oracle");
            Session.DefaultSession.AutoCreateOption = AutoCreateOption.SchemaOnly;
            Session.DefaultSession.Connect();
读取服务器时间:
public static  class oracleGetsysDate {
        public static  DateTime sysDate()
        {
             System.Data.IDbCommand command;
            //System.Data.IDataReader reader;
            command = DevExpress.Xpo.Session.DefaultSession.Connection.CreateCommand();
           
            command.CommandText = "Select sysdate from dual";
            //reader = command.ExecuteReader();
            string dtm = command.ExecuteScalar().ToString();
            DateTime jtdtm = DateTime.Parse(dtm);
            
            return jtdtm;
       }
调用:
  DateTime dt = oracleGetsysDate.sysDate().Date;
XPO的业务类:
public class TEST : XPLiteObject
    {
        string fDEPT_CODE;
        [Key]
        [Size(10)]
        [DbType("varchar2(30)")][Persistent("Dept_code")][DisplayName("科室代码")]
        public string DEPT_CODE
        {
            get { return fDEPT_CODE; }
            set { SetPropertyValue<string>("DEPT_CODE", ref fDEPT_CODE, value); }
        }
        string fDEPT_NAME;
        [Size(30)]
        public string DEPT_NAME
        {
            get { return fDEPT_NAME; }
            set { SetPropertyValue<string>("DEPT_NAME", ref fDEPT_NAME, value); }
        }
        DateTime fCREATEDATE;
        public DateTime CREATEDATE
        {
            get { return fCREATEDATE; }
            set {
               
                SetPropertyValue<DateTime>("CREATEDATE", ref fCREATEDATE, value); }
        }
        public TEST(Session session) : base(session) { }
        public TEST() : base(Session.DefaultSession) { }
        public override void AfterConstruction() { base.AfterConstruction(); }
        protected override void OnSaving()
        {
            base.OnSaving();
            if (!IsDeleted)
            {
                UnitOfWork uw = new UnitOfWork();
               
                if (fDEPT_CODE == null || fDEPT_CODE == "")
                    throw new Exception("科室代码不能置空值,保存失败!");
            
            }
        }
        protected override void Spoil(bool disposing)
        {
            base.Spoil(disposing);
        }
    }
调用XPO:
  try
            {
                DateTime dt = oracleGetsysDate.sysDate().Date;
                dbserver.TEST test = new TEST();
                test.DEPT_CODE = "123";
                test.DEPT_NAME = "Test2";
                test.CREATEDATE = dt;
               
                test.Save();
               // DateTime dt = oracleGetsysDate.sysDate();
                //this.textBox1.Text = dt.ToString();//得到查询表的第一行第一列
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示");
            }


回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-4-24 21:25

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表