Recently, I started to contact nhib.pdf. I had a lot of twists and turns to use it for the first time. Finally, I made a smooth call. This document records the process and hopes to help later users (my development environment is vs2008sp1 + MSSQL 2000 ).
1. Download The nhib.pdf framework first. I use version 2.1;
2. Create an Asp.net Web Application
Program Nhtest and add reference:
Nhib.pdf. dll, nhib.pdf. bytecode. linfu. dll, linfu. dynamicproxy. dll
3. modify web. config to configure nhib.pdf. Add the following to the configsections node:
< Section Name = "Hibernate-configuration"
Type = "Nhibernate. cfg. configurationsectionhandler, nhibler" />
Then, add the following nodes at the same level as configsections:
Code
< Hibernate-Configuration Xmlns = "Urn: nhibernate-configuration-2.2" >
< Session-factory Name = "Nhtest" >
< Property Name = "Connection. driver_class" > Nhib.pdf. Driver. sqlclientdriver </ Property >
< Property Name = "Connection. connection_string" >
Server = 127.0.0.1; uid = sa; Pwd = 123456; database = testcenter
</ Property >
< Property Name = "Adonet. batch_size" > 10 </ Property >
< Property Name = "Show_ SQL" > True </ Property >
< Property Name = "Dialect" > Nhib.pdf. dialect. mssql2000dialect </ Property >
< Property Name = "Use_outer_join" > True </ Property >
< Property Name = "Command_timeout" > 60 </ Property >
< Property Name = "Query. substitutions" > True 1, false 0, yes 'y', no 'n' </ Property >
< Property Name = "Proxyfactory. factory_class" > Nhibernate. bytecode. linfu. proxyfactoryfactory, Nhibernate. bytecode. linfu </ Property >
< Mapping Assembly = "Nhtest" />
</ Session-factory >
</ Hibernate-Configuration >
4. Create a new table named nhuser in the database:
5. Create nhuser. CSNhuser. CS
Using System;
Namespace Nhtest
{
Public Class Nhuser
{
Public Nhuser (){}
Public Virtual Int Userid
{ Get ; Set ;}
Public Virtual StringUsername
{Get;Set;}
Public virtual string nickname
{ Get ; set ;}
Public virtual string userpass
{ Get ; set ;}
Public virtual datetime regtime
{ Get ; set ;}
Public Virtual StringRegip
{Get;Set;}
}
}
6. Create nhtest. nhuser. HBM. xml under the site and compile it as an embedded Resource:Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Hibernate-Mapping Xmlns = "Urn: nhibernate-mapping-2.2" >
< Class Name = "Nhtest. nhuser, nhtest" Table = "Nhuser" >
< ID Name = "Userid" Column = "Userid" >
< Generator Class = "Native" />
</ ID >
< Property Name = "Username" />
< Property Name = "Nickname" />
< Property Name = "Userpass" />
< Property Name = "Regtime" />
< Property Name = "Regip" />
</ Class >
</ Hibernate-Mapping >
Note: Because userid is set to auto-increment ID in the database<GeneratorClass= "Native"/>To set it to native, otherwise, when identity_insert is set to off, the explicit value cannot be inserted into the ID column of the table "nh_user.
The following describes how to use it. Drag several controls on the default. aspx page:
Backend CSCode, Reference namespace:
Using nhib.pdf;
Using nhib.pdf. cfg;
Default. CS
Private Void Adduser (){
Configuration cfg = New Configuration ();
Cfg. addassembly ( " Nhtest " );
Isessionfactory Factory = Cfg. buildsessionfactory ();
Isession session = Factory. opensession ();
Itransaction Trans = Session. begintransaction ();
Nhuser u = New Nhtest. nhuser ()
{
Nickname = Tbnick. Text,
Username = Tbuname. Text,
Userpass = Tbpass. Text,
Regtime = Datetime. Now,
Regip = Request. userhostaddress
};
Session. Save (U );
Session. Flush ();
Trans. Commit ();
}
Protected VoidBtnok_click (ObjectSender, eventargs E)
{
Adduser ();
}
After testing, it runs normally.
Note: In this article, I found relevant information one by one after an exception occurs in almost every step. Exercise records the beginning of NH learning.