程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> NHibernate1.2 實現增、刪、改、查 功能!!!

NHibernate1.2 實現增、刪、改、查 功能!!!

編輯:.NET實例教程
說明:login 為數據庫表的實體類

    public class UserAccess
    {

        //構造函數
        public UserAccess()
        {
            cfg.AddAssembly("Entitys");
        }

        private NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
        private ISession session = null;    //會話工廠
        private ITransaction tran = null;     //事務處理
       
        private string m_error = "";

        /// <summary>
        /// 獲取錯誤信息
        /// </summary>
        public string Error
        {
            get { return this.m_error; }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public bool InsertUser(login l)
        {
            try
            {

                session = cfg.BuildSessionFactory().OpenSession();
                tran = session.BeginTransaction();
        

歡迎光臨學網,收藏本篇文章 [1] [2] [3] [4] [5] [6]

$False$

;        session.Save(l);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                this.m_error = ex.Message;
                return false;
            }
            finally
            {
               
                this.session.Close();
            }
            return true;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public bool UpdateUser(login l,int id)
        {
            try
            {
                session = cfg.BuildSessionFactory().OpenSession();
                tran = session.BeginTransaction();

文章整理:學網 http://www.xue5.com (本站) [1] [2] [3] [4] [5] [6]

                session.Update(l,id);
                tran.Commit();
            }
            catch(Exception ex)
            {
                tran.Rollback();
                this.m_error = ex.Message;
                return false;
            }       
            finally
            {
&nbs;               this.session.Close();
            }
            return true;
        }

        /// <summary>
        /// 刪除
        /// </summary>
        /// <returns></returns>
        public bool IDelUser(int ID)
        {
            try
            {
                session = cfg.BuildSessionFactory().OpenSession();
                tran = session.BeginTransaction();

         歡迎光臨學網,收藏本篇文章 [1] [2] [3] [4] [5] [6]

       login l = (login)session.Load(typeof(login), ID);
                session.Delete(l);
                tran.Commit();
            }
            catch(Exception ex)
            {
                tran.Rollback();
                this.m_error = ex.Message;
                return false;
            }       
            finally
            {
                this.session.Close();
            }
            return true;
        }

        /// <summary>
        /// 查找一條數據
        /// </summary>
        /// <returns></returns>
        public Entitys.login SelectUserByID(int ID)
        {
            try
            {
                session = cfg.BuildSessionFactory().OpenSession();
             歡迎光臨學網,點擊這裡查看更多文章教程 [1] [2] [3] [4] [5] [6]

   login l = (login)session.Load(typeof(login), ID);
                return l;
            }
            catch (Exception ex)
            {
                this.m_error = ex.Message;
                return null;
            }
            finally
            {
                this.session.Close();
            }
        }

        ///// <summary>
        ///// 查看
        ///// </summary>
        ///// <returns></returns>
        //public int GetUsers()
        //{
        //    try
        //    {
           
        //    }
        //    catch(Exception ex)
        //    {
        //        return -1;
        //        throw(ex);
        //    }
        //}
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved