Surance Center

Nhibernate 1.2.0.4 在VS2003 sqlserver2000 下测试通过

Download:
/Files/xxpyeippx/NHQuickStart.rar


Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   
    
<!-- Add this element -->
    
<configSections>
        
<section
            
name="hibernate-configuration"
            type
="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
    
</configSections>
    

    
<!-- Add this element -->
    
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        
<session-factory>
            
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
            
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
            
<property name="connection.connection_string">Server=192.168.0.17;initial catalog=Cat;user id=sa;password=sa123</property>
            
            
<mapping assembly="QuickStart" />
        
</session-factory>
    
</hibernate-configuration>


  
<system.web>

    
<!--  DYNAMIC DEBUG COMPILATION
          Set compilation debug="true" to enable ASPX debugging.  Otherwise, setting this value to
          false will improve runtime performance of this application. 
          Set compilation debug="true" to insert debugging symbols (.pdb information)
          into the compiled page. Because this creates a larger file that executes
          more slowly, you should set this value to true only when debugging and to
          false at all other times. For more information, refer to the documentation about
          debugging ASP.NET files.
    
-->
    
<compilation 
         
defaultLanguage="c#"
         debug
="true"
    
/>

    
<!--  CUSTOM ERROR MESSAGES
          Set customErrors mode="On" or "RemoteOnly" to enable custom error messages, "Off" to disable. 
          Add <error> tags for each of the errors you want to handle.

          "On" Always display custom (friendly) messages.
          "Off" Always display detailed ASP.NET error information.
          "RemoteOnly" Display custom (friendly) messages only to users not running 
           on the local Web server. This setting is recommended for security purposes, so 
           that you do not display application detail information to remote clients.
    
-->
    
<customErrors 
    
mode="RemoteOnly" 
    
/> 

    
<!--  AUTHENTICATION 
          This section sets the authentication policies of the application. Possible modes are "Windows", 
          "Forms", "Passport" and "None"

          "None" No authentication is performed. 
          "Windows" IIS performs authentication (Basic, Digest, or Integrated Windows) according to 
           its settings for the application. Anonymous access must be disabled in IIS. 
          "Forms" You provide a custom form (Web page) for users to enter their credentials, and then 
           you authenticate them in your application. A user credential token is stored in a cookie.
          "Passport" Authentication is performed via a centralized authentication service provided
           by Microsoft that offers a single logon and core profile services for member sites.
    
-->
    
<authentication mode="Windows" /> 

    
<!--  AUTHORIZATION 
          This section sets the authorization policies of the application. You can allow or deny access
          to application resources by user or role. Wildcards: "*" mean everyone, "?" means anonymous 
          (unauthenticated) users.
    
-->

    
<authorization>
        
<allow users="*" /> <!-- Allow all users -->
            
<!--  <allow     users="[comma separated list of users]"
                             roles="[comma separated list of roles]"/>
                  <deny      users="[comma separated list of users]"
                             roles="[comma separated list of roles]"/>
            
-->
    
</authorization>

    
<!--  APPLICATION-LEVEL TRACE LOGGING
          Application-level tracing enables trace log output for every page within an application. 
          Set trace enabled="true" to enable application trace logging.  If pageOutput="true", the
          trace information will be displayed at the bottom of each page.  Otherwise, you can view the 
          application trace log by browsing the "trace.axd" page from your web application
          root. 
    
-->
    
<trace
        
enabled="false"
        requestLimit
="10"
        pageOutput
="false"
        traceMode
="SortByTime"
        localOnly
="true"
    
/>

    
<!--  SESSION STATE SETTINGS
          By default ASP.NET uses cookies to identify which requests belong to a particular session. 
          If cookies are not available, a session can be tracked by adding a session identifier to the URL. 
          To disable cookies, set sessionState cookieless="true".
    
-->
    
<sessionState 
            
mode="InProc"
            stateConnectionString
="tcpip=127.0.0.1:42424"
            sqlConnectionString
="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless
="false" 
            timeout
="20" 
    
/>

    
<!--  GLOBALIZATION
          This section sets the globalization settings of the application. 
    
-->
    
<globalization 
            
requestEncoding="utf-8" 
            responseEncoding
="utf-8" 
   
/>
   
 
</system.web>

</configuration>

POCO:
namespace QuickStart
{
    
public class Cat
    
{
        
private string id;
        
private string name;
        
private char   sex;
        
private float  weight;

        
public Cat()
        
{
        }


        
public virtual string Id
        
{
            
get return id; }
            
set { id = value; }
        }


        
public virtual string Name
        
{
            
get return name; }
            
set { name = value; }
        }


        
public virtual char Sex
        
{
            
get return sex; }
            
set { sex = value; }
        }


        
public virtual float Weight
        
{
            
get return weight; }
            
set { weight = value; }
        }

    }

}



Mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace
="QuickStart" assembly="QuickStart">

    
<class name="Cat" table="Cat">

        
<!-- A 32 hex character is our surrogate key. It's automatically
            generated by NHibernate with the UUID pattern. 
-->
        
<id name="Id">
            
<column name="CatId" sql-type="char(32)" not-null="true"/>
            
<generator class="uuid.hex" />
        
</id>

        
<!-- A cat has to have a name, but it shouldn' be too long. -->
        
<property name="Name">
            
<column name="Name" length="16" not-null="true" />
        
</property>
        
<property name="Sex" />
        
<property name="Weight" />
    
</class>

</hibernate-mapping>

Code1:
using System;
using System.Web;
using NHibernate;
using NHibernate.Cfg;

namespace QuickStart
{
    
public sealed class NHibernateHelper
    
{
        
private const string CurrentSessionKey = "nhibernate.current_session";
        
private static readonly ISessionFactory sessionFactory;

        
static NHibernateHelper()
        
{
            sessionFactory 
= new Configuration().Configure().BuildSessionFactory();
            
        }


        
public static ISession GetCurrentSession()
        
{
            HttpContext context 
= HttpContext.Current;
            ISession currentSession 
= context.Items[CurrentSessionKey] as ISession;

            
if (currentSession == null)
            
{
                currentSession 
= sessionFactory.OpenSession();
                context.Items[CurrentSessionKey] 
= currentSession;
            }


            
return currentSession;
        }


        
public static void CloseSession()
        
{
            HttpContext context 
= HttpContext.Current;
            ISession currentSession 
= context.Items[CurrentSessionKey] as ISession;

            
if (currentSession == null)
            
{
                
// No current session
                return;
            }


            currentSession.Close();
            context.Items.Remove(CurrentSessionKey);
        }


        
public static void CloseSessionFactory()
        
{
            
if (sessionFactory != null)
            
{
                sessionFactory.Close();
            }

        }

    }

}



Code2

using System;
using System.Collections;
using System.ComponentModel;
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;

using System.Reflection;

namespace QuickStart
{
    
/// <summary>
    
/// Summary description for WebForm1.
    
/// </summary>

    public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Button Button2;
        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
        
protected System.Web.UI.WebControls.Button Button1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// Put user code to initialize the page here
        }


        
Web Form Designer generated code

        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            ISession session 
= NHibernateHelper.GetCurrentSession();

            ITransaction tx 
= session.BeginTransaction();

            Cat princess 
= new Cat();
            princess.Name 
= "Princess";
            princess.Sex 
= 'F';
            princess.Weight 
= 7.4f;

            session.Save(princess);
            tx.Commit();

            NHibernateHelper.CloseSession();
        }


        
private void Button2_Click(object sender, System.EventArgs e)
        
{ISession session = NHibernateHelper.GetCurrentSession();
            ITransaction tx 
= session.BeginTransaction();

            IQuery query 
= session.CreateQuery("select c from Cat as c where c.Sex = :sex");
            query.SetCharacter(
"sex"'F');

        
            
            
this.DataGrid1.DataSource =query.List();
            
this.DataGrid1.DataBind();

            tx.Commit();
        }





        
    }

}




posted @ 2007-08-21 16:10  xxp  阅读(430)  评论(0编辑  收藏  举报
Surance Center