• NHibernate


    项目结构图。

    首先引用程序集,在lib文件夹下的程序集事都要用到的,之后在引用中引用它们。

    然后是App.config配置文件:

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 
     3 <!--暂时只能有这么多代码-->
     4 <configuration>
     5 
     6   <configSections>
     7     <section name ="hibernate-configuration" type ="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"/>
     8   </configSections>
     9 
    10   <hibernate-configuration  xmlns ="urn:nhibernate-configuration-2.2">
    11     <session-factory>
    12       <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
    13       <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    14       <property name="connection.connection_string">Server=HUO-PCSQLEXPRESS;initial catalog=NHibernate;Integrated Security=SSPI</property>
    15       <property name="adonet.batch_size">10</property>
    16       <property name="show_sql">true</property> <!--是不是显示SQL语句-->
    17       <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    18       <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    19     </session-factory>
    20   </hibernate-configuration>
    21   
    22 </configuration>

    如果是用App.config文件那么,在Load加载时间中的代码应该是这样写:

    1             //读取配置文件
    2             //读取所有映射文件
    3             Configuration config = new Configuration().AddAssembly("Test");

    如果是使用Nhibernate.cfg.xml文件,那么代码应该这样写:

    1             //读取配置文件
    2             //读取所有映射文件
    3             //如果使用这种方法,那么要在项目中添加 NHibernate.cfg.xml 文件
    4             //如果是放在项目中的文件夹中,那么前面要加上文件夹的名称,比如 @"Config/NHibernate.cfg.xml"
    5             Configuration config = new Configuration().Configure(@"Config/NHibernate.cfg.xml");

    运行Form1窗口:

    主要代码:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Threading.Tasks;
      9 using System.Windows.Forms;
     10 
     11 //引入对应命名空间
     12 using NHibernate;
     13 using NHibernate.Cfg;
     14 using Test;
     15 
     16 namespace WinFormTest
     17 {
     18     public partial class Form1 : Form
     19     {
     20         ISession session = null;
     21         ISessionFactory factory = null;
     22         ITransaction trans = null;
     23 
     24         public Form1()
     25         {
     26             InitializeComponent();
     27         }
     28 
     29         /// <summary>
     30         /// 窗体加载事件
     31         /// </summary>
     32         private void Form1_Load(object sender, EventArgs e)
     33         {
     34             //读取配置文件
     35             //读取所有映射文件
     36             Configuration config = new Configuration().AddAssembly("Test");
     37             
     38             //创建Session工厂,负责持久化的连接以及OR映射
     39             factory = config.BuildSessionFactory();
     40 
     41             //创建一个可用于用户级别的操作对象。
     42             session = factory.OpenSession();
     43         }
     44 
     45         /// <summary>
     46         /// 添加事件
     47         /// </summary>
     48         private void btn_Insert_Click(object sender, EventArgs e)
     49         {
     50             //开启事务对象
     51             trans = session.BeginTransaction();
     52 
     53             try
     54             {
     55                 Person p = new Person();
     56                 p.Name = this.txt_Name.Text.Trim();
     57 
     58                 //将对象保存到数据库
     59                 //将对象P必须转换成数据库能识别的SQL语句
     60                 //由于IsessionFactory已经保存了所有的OR映射关系
     61                 //Isession能根据相对应的方言来实现SQL语句
     62                 session.Save(p);
     63 
     64                 trans.Commit();
     65 
     66                 MessageBox.Show("添加成功!", "提示");
     67             }
     68             catch (Exception)
     69             {
     70                 trans.Rollback();
     71             }
     72         }
     73 
     74         /// <summary>
     75         /// 删除事件
     76         /// </summary>
     77         private void btn_Delete_Click(object sender, EventArgs e)
     78         {
     79             //开启事务
     80             trans = session.BeginTransaction();
     81 
     82             try
     83             {
     84                 //获取要删除的对象
     85                 Person p = (Person)session.Get(typeof(Person), Convert.ToInt32(this.txt_ID.Text.Trim()));
     86 
     87                 session.Delete(p);
     88 
     89                 trans.Commit();
     90 
     91                 MessageBox.Show("删除成功!", "提示");
     92             }
     93             catch (Exception)
     94             {
     95                 trans.Rollback();
     96             }
     97         }
     98 
     99         /// <summary>
    100         /// 修改事件
    101         /// </summary>
    102         private void btn_Update_Click(object sender, EventArgs e)
    103         {
    104             //开启事务对象
    105             trans = session.BeginTransaction();
    106 
    107             try
    108             {
    109                 //获取要修改的对象
    110                 Person p = (Person)session.Get(typeof(Person), Convert.ToInt32(this.txt_ID.Text.Trim()));
    111 
    112                 //更改对象的值
    113                 p.Name = this.txt_Name.Text.Trim();
    114 
    115                 //把更改的数据,重新提交数据库,用以实现更新
    116                 session.Save(p);
    117 
    118                 //提交事务
    119                 trans.Commit();
    120 
    121                 MessageBox.Show("修改成功!", "提示");
    122             }
    123             catch (Exception)
    124             {
    125                 //如果出现错误,回滚事务
    126                 trans.Rollback();
    127             }
    128         }
    129 
    130         /// <summary>
    131         /// 查找事件
    132         /// </summary>
    133         private void btn_Select_Click(object sender, EventArgs e)
    134         {
    135             //不需要开启事务
    136             Person p = (Person)session.Get(typeof(Person), Convert.ToInt32(this.txt_ID.Text.Trim()));
    137 
    138             this.txt_Name.Text = p.Name.ToString();
    139         }
    140     }
    141 }



    项目Demo:
    使用App.config文件的:百度云,NHibernate文件夹-->NHibernateTest-App.config
    使用NHibernate.cfg.xml文件的:百度云,NHibernate文件夹-->NHibernateTest- .cfg.xml

  • 相关阅读:
    创建者模式 -- 单例模式(反射&序列化)
    设计模式(总)
    并不是static final 修饰的变量都是编译期常量
    内部类和静态内部类的加载顺序
    所有的Java虚拟机必须实现在每个类或接口被Java程序 “ 首次主动使用 ” 时才初始化他们
    24 shell 管道命令与过滤器
    26 bash shell中的信号
    25 Linux中的信号
    23 shell 进程替换
    22 shell组命令与子进程
  • 原文地址:https://www.cnblogs.com/KTblog/p/4508458.html
Copyright © 2020-2023  润新知