Nhibernate例[导入]
/ 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;
//前台
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();
}
}
}
配置文件
表的配置文件
源文件:代码很乱..
https://files.cnblogs.com/wujun/hibernate.rar
/**/ /// 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();
}
{
//添家字段
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>
<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>
<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>
源文件:代码很乱..
https://files.cnblogs.com/wujun/hibernate.rar
posted on 2006-08-19 19:59 hello csharp 阅读(1098) 评论(5) 编辑 收藏 举报