NHibernate 2003下实例

一共4个文件
1)message.aspx.cs

using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NHibernate;
using NHibernate.Cfg;

namespace ServiceTest
{
 /// <summary>
 /// message 的摘要说明。
 /// </summary>
 public class message : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.ListBox ListBox1;
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.Button Button1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!this.Page.IsPostBack)
   {
    this.QueryMessages();
   }
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private ISessionFactory factory;
  private ISession session;

  private void QueryMessages()
  {
   Configuration cfg = new Configuration();
   cfg.AddAssembly("ServiceTest");

   try
   {
    factory = cfg.BuildSessionFactory();
   }
   catch(Exception e)
   {
    Response.Write(e.Message);
   }
   

   session = factory.OpenSession();

   IList messages = session.CreateCriteria(typeof(Message)).List();
   this.ListBox1.Items.Clear();
   foreach(Message msg in messages)
   {
    this.ListBox1.Items.Add(msg.ToString());
   }
   session.Clear(); 

  }
 }
}

2)message.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="ServiceTest.Message, ServiceTest" table="tbl_area">
         <id name="Id" column="id" type="Int32">
              <generator class="identity" />
         </id>
         <property name="areaname" type="String" length="20"/>
     </class>
</hibernate-mapping>

3)message.cs

using System;

namespace ServiceTest
{
 /// <summary>
 /// Message 的摘要说明。
 /// </summary>
 public class Message
 {
  public Message()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  private int _id;
  private string _areaname;
  /// <summary>
  ///
  /// </summary>
  public virtual  int Id
  {
   set{ _id=value;}
   get{return _id;}
  }
  /// <summary>
  ///
  /// </summary>
  public virtual  string areaname
  {
   set{ _areaname=value;}
   get{return _areaname;}
  }

  public override string ToString()
  {
   return _areaname;
  }


 }
}

4)web.config

<configuration>
 <configSections>
  <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
 </configSections>
 <nhibernate>
        <add key="hibernate.show_sql"
             value="true" />
        <add key="hibernate.connection.provider"         
             value="NHibernate.Connection.DriverConnectionProvider" />
        <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="data source=.;initial catalog=soucou;user id=sa;pwd=sa;" />
    </nhibernate>

你可能感兴趣的:(Hibernate)