用Nhibernate实现数据的添加、删除、修改

时间:2022-07-30 00:56:20
Nhibernate怎么实现数据的添加、删除、修改简单程序   一、创建数据库 数据库名:Nhibernate
u
se NHibernate
go
CREATE TABLE users ( 
  LogonID nvarchar(20) NOT NULL default '0',
 Name nvarchar(40) default NULL,
 Password nvarchar(20) default NULL,
 EmailAddress nvarchar(40) default NULL,
 PRIMARY KEY (LogonID)
)
go
数据表:users   二、总体介绍 项目名:WebNhibernate 界面:WebForm.aspx 具体表现文件:WebForm.aspx.cs 实体类文件:EntityClass.cs 映射文件:Userhbm.xml 配置文件:Web.config   三、创建Web界面
类型 对象名 Text属性值
Label Label1 ID:
Label Label2 姓名:
Label Label3 密码:
Label Label4 Email
Label Labmessage  
TextBox TxtId  
TextBox TxtName  
TextBox TxtPassword  
TextBox TxtEmail  
Button ButSave 添加
Button ButDel 删除
Button ButUpdata 修改
  四、创建映射文件(xml文件)和实体类 实体类 using System; namespace WebNhibernate {      public class EntityClass      {           private string id;           private string userName;           private string password;           private string emailAddress;          public EntityClass()          {}          public string Id          {               get { return id; }               set { id = value; }          }          public string UserName          {               get { return userName; }               set { userName = value; }          }          public string Password          {               get { return password; }               set { password = value; }          }          public string EmailAddress          {               get { return emailAddress; }               set { emailAddress = value; }          }      } } 映射文件: <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">      <class name="WebNhibernate.EntityClass, WebNhibernate" table="users">          <id name="Id" column="LogonId" type="String" length="20">               <generator class="assigned" />          </id>           <property name="UserName" column= "Name" type="string" length="40"/>           <property name="Password" type="string" length="20"/>           <property name="EmailAddress" type="String" length="40"/>      </class> </hibernate-mapping> 注意点: 1<class name="WebNhibernate.EntityClass, WebNhibernate" table="users">        WebNhibernate.EntityClass代表:实体类名     WebNhibernate代表:该项目的装配集名称       Users代表:数据表名 2.当属性列表<property name=”” column=””/>中既有name和column说明实体层的属性与数据表的字段名不同名 3.指定一个id, 在数据表中就是主键, 这个非常重要,nhibernate就是通过id来判断对象的唯一性的.   五、在配置文件中添加配置内容 1.首先在配置文件的<configuration>代码下面添加如下代码        <configSections>              <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />      </configSections>       这一段代码是必须要的 2.在配置文件的</system.web>代码下面添加如下代码 <nhibernate>          <!连接数据提供者 -->          <add               key="hibernate.connection.provider"                        value="NHibernate.Connection.DriverConnectionProvider"          />          <!连接数据方言最常用的是MsSql2000Dialect -->          <add               key="hibernate.dialect"                                    value="NHibernate.Dialect.MsSql2000Dialect"          />          <!连接数据驱动类-->          <add               key="hibernate.connection.driver_class"                        value="NHibernate.Driver.SqlClientDriver"          />          <!连接数据库-->          <add               key="hibernate.connection.connection_string"               value="Server=yanfa1;initial catalog=nhibernate;User id=sa;password=8626798;"          /> </nhibernate>   六、实现代码
首先在文件头添加代码
using NHibernate;
using NHibernate.Cfg;
1.添加数据:
双击“添加“按钮         private void ButSave_Click(object sender, System.EventArgs e)          {               mCfg=new Configuration();//创建配置类               mCfg.AddXmlFile (System.Web.HttpContext.Current.Server.MapPath("Userhbm.xml"));//指明映射文件Userhbm.xml               EntityClass vEntity=new EntityClass();               vEntity.Id=TxtId.Text;               vEntity.UserName=TxtName.Text;               vEntity.Password=TxtPassword.Text;               vEntity.EmailAddress=TxtEmail.Text;               ISession vSession= mCfg.BuildSessionFactory().OpenSession();//创建会话工厂, 一般来说应该使用一个单例对象来封装会话工厂.               ITransaction vTransaction = vSession.BeginTransaction();//创建事物处理               try               {                    vSession.Save(vEntity);//向数据库添加数据                    vTransaction.Commit();                    Labmessage.Text="OK";               }               catch(Exception ex)               {                    vTransaction.Rollback();                    Labmessage.Text="Error"+ex.ToString();               }               finally               {                    vSession.Close();               }          }   2.删除数据:
双击“删除“按钮
        private void ButDel_Click(object sender, System.EventArgs e)
         {               mCfg=new Configuration();               mCfg.AddXmlFile (System.Web.HttpContext.Current.Server.MapPath("Userhbm.xml"));               ISession vSession= mCfg.BuildSessionFactory().OpenSession();               ITransaction vTransaction = vSession.BeginTransaction();               try               {                    EntityClass vEntity=(EntityClass) vSession.Load(typeof(EntityClass),TxtId.Text);//查找数据表中所要记录                    vSession.Delete(vEntity);//向数据库删除数据                    vTransaction.Commit();                    Labmessage.Text="OK";               }               catch(Exception ex)               {                    vTransaction.Rollback();                    Labmessage.Text="Error";               }               finally               {                    vSession.Close();               }          }   3.修改代码:
双击“修改“按钮
        private void ButUpdata_Click(object sender, System.EventArgs e)
         {               mCfg=new Configuration();               mCfg.AddXmlFile (System.Web.HttpContext.Current.Server.MapPath("Userhbm.xml"));               ISession vSession= mCfg.BuildSessionFactory().OpenSession();               ITransaction vTransaction = vSession.BeginTransaction();               try               {                    EntityClass vEntity=(EntityClass) vSession.Load(typeof(EntityClass),TxtId.Text);                    vEntity.UserName=TxtName.Text;                    vEntity.Password=TxtPassword.Text;                    vEntity.EmailAddress=TxtEmail.Text;                    vSession.Update(vEntity); //向数据库修改数据                    vTransaction.Commit();                    Labmessage.Text="OK";               }               catch(Exception ex)               {                    vTransaction.Rollback();                    Labmessage.Text="Error";               }               finally               {                    vSession.Close();               }          }