Basic use and Quick Start of nhib.pdf-Microsoft. NET development framework application resource (zhuan)

Source: Internet
Author: User
Tags log4net

Step 1:
We need the nhib.pdf assembly:
Nhib.pdf. dll
Log4net. dll
Iesi. Collections. dll
Castle. DynamicProxy. dll
At least four, of which

Nhib.pdf. dll is the core assembly,

Log4net. dll is a logging assembly,

Iesi. Collections. dll is the assembly of the Collection framework,

Castle. DynameicProxy. dll controls the reversed assembly

Note: You must copy four dll files at the same time. Otherwise, the following error occurs: failed to Load file or assembly "Iesi. collections, Version = 1.0.0.3, Culture = neutral "or one of its dependencies. Why do I need to load Iesi? Why do I need to load log4net?

Step 2:
Prepare persistence class and entity class. It is the ing object of O/R Mapping and encapsulates our data by controlling it.

Step 3:
Use the nhib.pdf configuration file
Specify the database connection mode in the configuration file.
Let nhib.pdf know how to connect to the database

Step 4:
Write the O/R Mapping File
Let nhib.pdf know how to map the relationship between. NET and the database

Finally, use the powerful nhib.pdf to compile your program!
To let NHibernate know how to connect to the database, you must configure it in app. config or web. config in. NET:

<ConfigSections> <! -- Configuration section -->
<Section name = "nhib.pdf"
Type = "System. Configuration. NameValueSectionHandler, System, Version = 1.0.5000.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"/>
</ConfigSections>

<! -- The configuration information is used to specify how NH accesses the database. -->
<Nhib.pdf>
<! -- Connection provider. The value must be the full name of the class that implements the IConnectionProvider interface -->
<Add key = "hibernate. connection. provider"
Value = "nhib.pdf. Connection. DriverConnectionProvider"/>
<! -- Database Dialect. The value must be the full name of the inherited Dialect class. -->
<Add key = "hibernate. dialect"
Value = "nhibect. Dialect. MsSql2000Dialect"/>
<! -- Data driver class. The value must be the full name of the class that implements the IDriver interface -->
<Add key = "hibernate. connection. driver_class"
Value = "nhib.pdf. Driver. SqlClientDriver"/>
<! -- Connection string. The value must correspond to the data driver class specified by driver_class. -->
<Add key = "hibernate. connection. connection_string"
Value = "server = (local); uid = sa; pwd = sa; database = dbtest"/>
</Nhib.pdf>

Create a persistent class:

Public class Account// Note that the attribute must be set to virtual
{
Private int id;

Virtual public int Id
{
Get {return id ;}
Set {id = value ;}
}
Private string username;

Virtual public string Username
{
Get {return username ;}
Set {username = value ;}

 

}
Private string password;

Virtual public string Password
{
Get {return password ;}
Set {password = value ;}
}
}

Write configurations for each persistence class
<? Xml version = "1.0" encoding = "UTF-8"?>
<Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.2">
<Class name = "BE. Account, BE" table = "account">
<Id name = "Id" column = "userid" type = "Int32">
<Generator class = "identity"/>
</Id>
<Property name = "Username" type = "String" length = "50"/>
<Property name = "Password" type = "String" length = "50"/>
</Class>
</Hibernate-mapping>

Xmlns specifies the version of nhib.pdf,
The name attribute of the class element specifies the full name of the persistence class. The names of the Assembly are separated by commas (,).
The table attribute specifies the name of the table in the database.
The id element specifies the primary key column name. The name attribute indicates the attribute name of the persistence class, and the column is the column name of the table.
The property element is the attribute corresponding to each other column of the persistence class.
The class attribute in the generator element specifies the auto-increment column.

Save as Account. hbm. xml file

Start using nhib.pdf

Using nhib.pdf;
Using nhib.pdf. Cfg;

Configuration cfg = new Configuration ();
Cfg. AddAssembly ("BE ");
ISessionFactory factory = cfg. BuildSessionFactory ();
ISession session = factory. OpenSession ();
ITransaction transaction = session. BeginTransaction ();
Account a = new Account ();
A. Username = "Guilee ";
A. Password = "1234 ";
A. Id = 1;
Session. Save ();
// Modify the database content
// Session. Update ();
// Delete the database content
// Session. Delete ();
Transaction. Commit ();
Session. Close ();
First, create a Configuration object
The Configuration object can parse mappings between all. Net objects and the background database.
Configuration cfg = new Configuration ();
Cfg. AddAssembly ("BE ");
The Configuration object searches for any files ending with hbm. xml in the Assembly. There are other ways to load the ing file, but this method is the easiest.
Create a Session Object
The ISession object provides a connection to the background database. The ITransaction object provides a transaction that can be managed by nhib.pdf.
ISessionFactory factory = cfg. BuildSessionFactory (); ISession session = factory. OpenSession ();
ITransaction transaction = session. BeginTransaction ();
Next, add, delete, modify, and query your objects.
Now you can use the traditional. Net Method to manipulate objects.
Nhib.pdf converts the query method into an object-oriented method. The simplest way to use the query is:
ICriteria c = session. CreateCriteria (typeof (BEAccount ));
C. Add (Expression. Eq ("Username", account. Username ));
C. Add (Expression. Eq ("Password", account. Password ));
IList list = c. List ();
ICriteria indicates a query action.
The entire code indicates:
Query the Account table and find all records whose username is account. Username and whose password is account. Password.
Use List to obtain all query results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.