blogjava's web log

          blogjava's web log
          ...

          Nhibernate例[導(dǎo)入]

          // vo


          /**/ /// ?create?table?tabletest(id?int,name?varchar(20),
          ????
          /// ?age?int,sex?varchar(20));

          ???? public ? class ?Tabletest
          ????
          {
          ????????
          private ? int ?id;
          ????????
          private ? string ?name;
          ????????
          private ? int ?age;
          ????????
          private ? string ?sex;


          //前臺
          private?void?button1_Click(object?sender,?System.EventArgs?e)
          ????????
          {
          ????????????
          //添家字段
          ????????????VO.Tabletest?vo=new?VO.Tabletest();
          ????????????vo.Id
          =int.Parse?(this.textBox1.Text?);
          ????????????vo.Name?
          =this.textBox2.Text?;
          ????????????vo.Age?
          =int.Parse(this.textBox3.Text?);
          ????????????vo.Sex?
          =this.textBox4.Text?;
          ????????????BLL.TableTestBll?bll
          =new?BLL.TableTestBll();
          ????????????bll.addTableTest(vo);
          ????????????
          this.dataGrid1?.DataSource=bll.getTableTest();
          ????????}


          ????????
          private?void?button2_Click(object?sender,?System.EventArgs?e)
          ????????
          {
          ????????????
          //刪除一條
          ????????????BLL.TableTestBll?bll=new?BLL.TableTestBll();
          ????????????
          string?srt=this.dataGrid1[this.dataGrid1.CurrentRowIndex,0].ToString();
          ????????????bll.remove(
          int.Parse?(srt));
          ????????????
          this.dataGrid1?.DataSource=bll.getTableTest();
          ????????}


          ????????
          private?void?button3_Click(object?sender,?System.EventArgs?e)
          ????????
          {
          ????????????
          //更新
          ????????????VO.Tabletest?vo=new?VO.Tabletest();
          ????????????vo.Id
          =int.Parse?(this.textBox1.Text?);
          ????????????vo.Name?
          =this.textBox2.Text?;
          ????????????vo.Age?
          =int.Parse(this.textBox3.Text?);
          ????????????vo.Sex?
          =this.textBox4.Text?;
          ????????????BLL.TableTestBll?bll
          =new?BLL.TableTestBll();
          ????????????bll.updataTable(vo);
          ????????????
          this.dataGrid1?.DataSource=bll.getTableTest();
          ????????}


          ????????
          private?void?button5_Click(object?sender,?System.EventArgs?e)
          ????????
          {
          ????????????
          //得到
          ????????????BLL.TableTestBll?bll=new?BLL.TableTestBll();
          ????????????
          this.dataGrid1.DataSource?=bll.getTableTest();
          ????????}




          using?System;
          using?NHibernate;
          using?System.Collections?;
          using?VO;

          namespace?BLL
          {
          ????
          ///?<summary>
          ????
          ///?Class1?的摘要說明。
          ????
          ///?</summary>

          ????public?class?TableTestBll
          ????
          {
          ????????
          public?TableTestBll()
          ????????
          {
          ????????}


          ????????
          public?NHibernate.ISession?getSession()
          ????????
          {
          ????????????NHibernate.Cfg.Configuration?cfg
          =new?NHibernate.Cfg.Configuration();
          ????????????cfg.Configure();
          ????????????NHibernate.ISessionFactory?sess
          =cfg.BuildSessionFactory();
          ????????????NHibernate.ISession?iss
          =sess.OpenSession();
          ????????????
          return?iss;
          ????????}

          ????????
          public?System.Collections.IList?getTableTest()
          ????????
          {
          ????????????NHibernate.ISession?sess
          =this.getSession();
          ????????????IList?list
          =sess.Find("from?Tabletest?t");
          ????????????
          return?list;
          ????????}

          ????????
          public?void?addTableTest(VO.Tabletest?vo)
          ????????
          {
          ????????????NHibernate.ISession?sess
          =this.getSession();
          ????????????sess.Save(vo,vo.Id);
          ????????????sess.Flush();
          ????????}

          ????????
          public?void?remove(int?id)
          ????????
          {
          ????????????NHibernate.ISession?sess
          =this.getSession();
          ????????????sess.Delete?(
          "from?Tabletest?t?where?t.Id="+id);
          ????????????sess.Flush();
          ????????}

          ????????
          public?void?updataTable(Tabletest?t)
          ????????
          {
          ????????????NHibernate.ISession?sess
          =this.getSession();
          ????????????Tabletest?tt
          =(Tabletest)sess.Load(t.GetType(),t.Id);
          ????????????tt.Name?
          =t.Name?;
          ????????????tt.Age?
          =t.Age?;
          ????????????tt.Sex?
          =t.Sex?;
          ????????????sess.SaveOrUpdate(tt);
          ????????????sess.Flush();
          ????????}

          ????}

          }



          配置文件

          <?xml?version="1.0"?encoding="utf-8"??>
          <hibernate-configuration??xmlns="urn:nhibernate-configuration-2.0"?>
          ????
          <session-factory?name="NHibernate.Test">
          ????????
          ????????
          <property?name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
          ????????
          <property?name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
          ????????
          <property?name="connection.connection_string">Server=.;initial?catalog=student1;User?Id=sa;Password=</property>
          ????????
          <property?name="show_sql">true</property>
          ????????
          <property?name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
          ????????
          <property?name="use_outer_join">true</property>
          ????????
          ????????
          <property?name="query.substitutions">true?1,?false?0,?yes?1,?no?0</property>
          ????????
          <mapping?file="personVO.hbm.xml"?/>
          ????????????
          ????
          </session-factory>
          ????
          </hibernate-configuration>

          表的配置文件
          <?xml?version="1.0"?encoding="utf-8"??>
          <hibernate-mapping?xmlns="urn:nhibernate-mapping-2.0">
          ????
          <class?name="VO.Tabletest,VO"?table="Test">
          ????????
          <id?name="Id"?column="id">
          ????????????
          <generator?class="native"/>
          ????????
          </id>
          ????????
          <property?name="Name"?column="name"/>
          ????????
          <property?name="Age"?column="age"/>
          ????????
          <property?name="Sex"?column="sex"/>
          ????
          </class>
          </hibernate-mapping>

          posted on 2006-05-28 14:54 record java and net 閱讀(372) 評論(0)  編輯  收藏 所屬分類: dot net相關(guān)

          導(dǎo)航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動(dòng)態(tài)語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 南木林县| 绥阳县| 安福县| 青阳县| 吕梁市| 兴国县| 临颍县| 海南省| 梁平县| 简阳市| 广东省| 乐昌市| 棋牌| 湾仔区| 蒙山县| 新干县| 汉沽区| 万州区| 砀山县| 措美县| 东阿县| 大厂| 咸宁市| 咸阳市| 张家川| 万年县| 长顺县| 昌图县| 桓台县| 太康县| 濮阳市| 德江县| 墨竹工卡县| 德庆县| 涪陵区| 肇庆市| 双鸭山市| 成武县| 承德市| 晋城| 岗巴县|