Ejemplo n.º 1
0
        /// <summary>
        /// 取得T物件
        /// </summary>
        /// <param name="oid">主鍵</param>
        public T SelectByKey <T>(Object oid)
        {
            //開啟連結
            ISession session = HibernateHelper.GetCurrentSession();

            try
            {
                return(session.Get <T>(oid));
            }
            //例外
            catch (Exception e)
            {
                log.Error("SelectByKey.error " + e.Message);
                log.Error("SelectByKey.oid " + oid);
                throw e;
            }
            finally
            {
                //關閉
                HibernateHelper.CloseSession();
            }
        }
Ejemplo n.º 2
0
        //2010/06/28 holer add 取得資料庫資料(本地sql) start
        /// <summary>
        /// 取得資料庫資料(本地sql)
        /// </summary>
        /// <param name="sql">資料庫查詢SQL(本地sql)</param>
        /// <returns>結果集</returns>
        public IList <Hashtable> SelectNativeSQL(string sql)
        {
            IList <Hashtable> list = new List <Hashtable>();
            //開啟連結
            ISession      session = HibernateHelper.GetCurrentSession();
            IDbConnection conn    = session.Connection;
            IDbCommand    cmd     = conn.CreateCommand();

            try
            {
                //查詢SQL
                cmd.CommandText = sql;
                cmd.Connection  = conn;
                //查詢結果
                IDataReader result = cmd.ExecuteReader();
                while (result.Read())
                {
                    Hashtable ht = new Hashtable();
                    for (int i = 0; i < result.FieldCount; i++)
                    {
                        ht.Add(result.GetName(i), result.GetValue(i).ToString());
                    }
                    //加入資料
                    list.Add(ht);
                }
                //關閉result
                if (result != null)
                {
                    result.Close();
                }
            }
            finally
            {
                //關閉連結
                HibernateHelper.CloseSession();
            }
            return(list);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 更新物件
        /// </summary>
        /// <param name="o">待更新的物件</param>
        public void ExecuteUpdate(Object o)
        {
            ISession     session = HibernateHelper.GetCurrentSession();
            ITransaction tr      = HibernateHelper.BeginTransaction();

            try
            {
                session.Merge(o);
                HibernateHelper.CommitTransaction();
            }
            //例外
            catch
            {
                HibernateHelper.RollbackTransaction();
                log.Error("更新" + o + "失敗!");
                throw;
            }
            //關閉連結
            finally
            {
                HibernateHelper.CloseSession();
            }
        }
Ejemplo n.º 4
0
        // <summary>
        /// 取得資料庫資料(本地sql)
        /// </summary>
        /// <param name="sql">資料庫查詢SQL(本地sql)</param>
        /// <returns>int 操作訊息(>0成功,否則失敗)</returns>
        public int updateNativeSQL(string sql)
        {
            int recode = -1;
            //開啟連結
            ISession      session = HibernateHelper.GetCurrentSession();
            IDbConnection conn    = session.Connection;
            IDbCommand    cmd     = conn.CreateCommand();

            try
            {
                //查詢SQL
                cmd.CommandText = sql;
                cmd.Connection  = conn;
                recode          = cmd.ExecuteNonQuery();
            }
            finally
            {
                //關閉連結
                HibernateHelper.CloseSession();
            }
            //操作訊息
            return(recode);
        }
Ejemplo n.º 5
0
        //2009/07/25 jackter end


        //2009/07/30 jackter add 單一查詢(本地sql)

        /// <summary>
        /// 單一查詢(本地sql)
        /// </summary>
        /// <param name="sql">sql</param>
        /// <returns>結果集</returns>
        public T SelectUniqueNativeSQL <T>(string sql)
        {
            //開啟連結
            ISession session = HibernateHelper.GetCurrentSession();

            try
            {
                //操作訊息
                ISQLQuery sqlQuery = session.CreateSQLQuery(sql);
                //已定義的實體類
                if (typeof(T).FullName.StartsWith("Com.Everunion"))
                {
                    return(sqlQuery.SetMaxResults(1)
                           .SetResultTransformer(Transformers.AliasToBean(typeof(T)))
                           .UniqueResult <T>());
                }
                //其它,如(T=object[])
                else
                {
                    return(sqlQuery.SetMaxResults(1).UniqueResult <T>());
                }
            }
            //拋出例外
            catch (Exception e)
            {
                log.Error("SelectUniqueNativeSQL.sql:" + sql);
                //輸出錯誤
                log.Error("SelectUniqueNativeSQL.error:" + e.ToString());
                throw e;
            }
            finally
            {
                //關閉
                HibernateHelper.CloseSession();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 查詢(不分頁)
        /// </summary>
        /// <param name="hql">hql</param>
        /// <returns>結果集</returns>
        public IList <T> Select <T>(string hql)
        {
            //開啟連結
            ISession session = HibernateHelper.GetCurrentSession();

            try
            {
                //操作訊息
                return(session.CreateQuery(hql).List <T>());
            }
            //拋出例外
            catch (Exception e)
            {
                log.Error("Select.hql:" + hql);
                //輸出錯誤
                log.Error("Select.error:" + e.ToString());
                throw e;
            }
            finally
            {
                //關閉
                HibernateHelper.CloseSession();
            }
        }
Ejemplo n.º 7
0
        //2009/06/25 dennis end


        //2009/07/01 dennis add 加入SelectPageObjct方法
        /// <summary>
        /// 單一查詢
        /// </summary>
        /// <param name="hql">hql</param>
        /// <returns>結果集</returns>
        public T SelectUnique <T>(string hql)
        {
            //開啟連結
            ISession session = HibernateHelper.GetCurrentSession();

            try
            {
                //操作訊息
                return(session.CreateQuery(hql).SetMaxResults(1).UniqueResult <T>());
            }
            //拋出例外
            catch (Exception e)
            {
                log.Error("GetUniqueObjct.sql:" + hql);
                //輸出錯誤
                log.Error("GetUniqueObjct.error:" + e.ToString());
                throw e;
            }
            finally
            {
                //關閉
                HibernateHelper.CloseSession();
            }
        }