How to use NHibernate to add, delete, and modify simple programs
First, create a database
Database Name: Nhibernateuse NHibernategoCREATE 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
Data table: Users
Second, the overall introduction
Project Name: WebnHibernate
Interface: WebMM.ASPX
Specific performance file: WebMM.ASPX.CS
Entity class file: EntityClass.cs
Mapping file: userhbm.xml
Profile: Web.config
Third, create a web interface
Types of
Object name
Text attribute value
Label
Label1
ID:
Label
Label2
Name:
Label
Label3
password:
Label
Label4
Email:
Label
LabMessage
Textbox
TXTID
Textbox
TXTNAME
Textbox
TXTPASSWORD
Textbox
TXTEMAIL
Button
Butsave
Add to
Button
Butdel
delete
Button
Butupdata
modify
Fourth, create a mapping file (XML file) and physical classes
Entity class
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 {returna usrname;}
Set {usrname = value;
}
Public String Password
{
Get {return password;}
Set {Password = Value;
}
Public String EmailAddress
{
Get {return emailaddress;}
Set {emailaddress = value;
}
}
}
Map file:
XML Version = "1.0" encoding = "UTF-8"?>
clas>
hibernate-maping>
be careful:
1.
WebnHibernate.EntityClass representative: entity class name
WebnHibernate Representative: The name of the assembly set of the project
Users representative: Data Name
2. When a list of attributes 3. Specify an ID, the primary key in the data table, this is very important, NHibernate is the uniqueness of the object through the ID. 5. Add configuration content in the configuration file 1. First add the following code below the
configsections> This code must be 2. Add the following code below the code of the configuration file Key = "hibernate.connection.provider" Value = "NHibernate.Connection.driverConnectionProvider" /> Key = "hibernate.diaract" Value = "nhibernate.diaract.msql2000diact" /> Key = "hibernate.connection.driver_class" Value = "NHibernate.driver.sqlclientDriver" /> Key = "hibernate.connection.connection_string" Value = "Server = yanfa1; initial catalog = NHibernate; user ID = sa; password = 8626798;" /> NHibernate> 6. Implement code First add code for the file header; use nhibitionnate.cfg; 1. Add data: Double-click the "Add" button Private Void Butsave_Click (Object Sender, System.EventArgs E) { McFG = new configuration (); // Create a configuration class Mcfg.addxmlfile (System.Web.httpContext.current.server.mappath ("UserHbm.xml")); // Indicates Map UserHbm.xml EntityClass Ventity = New EntityClass (); VENTITITITID = TXTID.TEXT; Ventity.username = txtname.text; VENTITY.PASSWORD = txtpassword.text; Ventity.emailAddress = txtemail.text; ISession vsession = mcfg.buildsessionFactory (). OpenSession (); // Creating a session factory, generally use a single case object to encapsulate session factories. ITransaction vTransaction = vsession.begintransaction (); // Create a thing Try { vSession.save (ventity); / / Add data to the database vTransaction.commit (); LabMessage.Text = "OK"; } Catch (Exception EX) { vTransaction.rollback (); LabMessage.Text = "Error" ex.toT7tring (); } Finally { vSession.close (); } } 2. Delete the data: Double-click "Delete" button 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; // Find what you want in the data table vSession.Delete (Ventity); // Remove data to the database vTransaction.commit (); LabMessage.Text = "OK"; } Catch (Exception EX) { vTransaction.rollback (); LabMessage.Text = "Error"; } Finally { vSession.close (); } } 3. Modify the code: Double-click "Modify" button 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); / / to modify data to the database vTransaction.commit (); LabMessage.Text = "OK"; } Catch (Exception EX) { vTransaction.rollback (); LabMessage.Text = "Error"; } Finally { vSession.close (); } } Because I have just just contacted NHibernate, there is still a lot of technical difficulties, I don't understand, I still need more effort, I hope to discuss with everyone.