NHibernate中DBSessionFactory写法

来源:互联网 发布:ug数控车编程教程视频 编辑:程序博客网 时间:2024/06/05 07:37

NHibernate中DBSessionFactory写法

/// <summary>    /// 定义: 会话工厂类    /// create date:2013-08    /// modify date:2013-12-31 by vp:hsg 可以从此类派生和重写SF方法加new重写    /// </summary>    public class DBSessionFactory     {        protected static ISessionFactory m_SessionFactory = null;        public static ISessionFactory SF        {            get            {                if (m_SessionFactory == null)                {                    string root = AppDomain.CurrentDomain.BaseDirectory;                    string cfgFile = System.IO.Path.Combine(root, "Config/hibernate.cfg.xml");                    var cfg = new NHibernate.Cfg.Configuration().Configure(cfgFile);                    m_SessionFactory = cfg.BuildSessionFactory();                }                return m_SessionFactory;            }            set            {                m_SessionFactory = value;            }        }        public static DataTable Query(string sql)        {            using (ISession s = SF.OpenSession())            {                IDbCommand command = s.Connection.CreateCommand();                command.CommandText = sql;                IDataReader reader = command.ExecuteReader();                DataTable result = new DataTable();                DataTable schemaTable = reader.GetSchemaTable();                for (int i = 0; i < schemaTable.Rows.Count; i++)                {                    result.Columns.Add(schemaTable.Rows[i][0].ToString(), Type.GetType(schemaTable.Rows[i]["DataType"].ToString()));                }                result.Load(reader);                return result;            }        }    }


 

0 0
原创粉丝点击