• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
鱼要的是海洋,鸟要的是天空,我要的是自由!
平凡的人不平凡的人生
博客园    首页    新随笔    联系   管理    订阅  订阅

nhibernate入门系列 NHB的MAP文件使用

MAP文件,可以充当一个数据库的描述,例如数据库的基本关系,类型的定义,一般情况下,当给出了MAP文件,您完全可以根据它生成对应的数据库.

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Tool.hbm2ddl;
using System ;
using System.Reflection ;

namespace FootBallGameClassLibrary.B2 {

   public class HibernateUtil {

      private static ISessionFactory sessions;
      private static Configuration cfg;
      private static Dialect dialect;

     private HibernateUtil() {
      }

     public static ISession OpenSession() {
        if (sessions == null){
            //创建Session工厂
            BuildSessionFactory();
         }
         return sessions.OpenSession();
      }

     private static void BuildSessionFactory() {
          ExportSchema( new string[] {"C注册用户.hbm.xml" ,
                "C代理用户.hbm.xml" ,  "C管理人员.hbm.xml" ,
                "C通告信息.hbm.xml" , } , false );

        //上面的程序是,第二个参数,如果是TRUE就表明要从MAP文件中提取信息并建立数据库
      }

       private static void ExportSchema(string[] files, bool exportSchema) {
          cfg = new Configuration();
          foreach (string file in files) {
               cfg.AddResource("你的程序集." + file, Assembly.Load("你的程序集"));
           }
           dialect = Dialect.GetDialect();
          if(exportSchema) new SchemaExport(cfg).Create(true, true);
           // 这句就是从,MAP文件中建立数据库 如果参数为TRUE就建立数据库.

          sessions = cfg.BuildSessionFactory( );
      }

     public void DropSchema() {
          new SchemaExport(cfg).Drop(true, true);
          //这个就不用我说了吧,大家一看就明白,我只是在这儿给大家表记一下
      }
   }
}

posted @ 2007-01-24 01:41  伊凡  阅读(377)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3