NHibernate with Microsoft Access (Jet) Sample
This sample solution demonstrates NHibernate with Microsoft Access (Jet). The code demonstrates:
- How to configure NHibernate to use an Access (Jet) database.
- How to disable aggressive connection closing to ensure you can use AutoNumber fields.
- How to create a log file containing NHibernate debugging messages.
The sample is distributed as a Visual Studio 2005 solution with C#. The sample requires NHibernate 1.2.0.CR1 (distributed separately).
Download
NHibernateWithAccess.zip 或者http://download.csdn.net/source/2445341(22kb)
Instructions
1. Download NHibernate
If you have not done so already, go to http://www.hibernate.org/6.html and look for the NHibernate binary release. This sample was compiled against 1.2.0.CR1 (the latest version at the time this page was created). The sample will be updated whenever NHibernate is updated.
2. Install NHibernate
The installer will suggest a default location of C:/Program Files/NHibernate. You can select a different folder but make sure you remember the location. You will need to browse to the folder after opening the sample solution (in order to correct the NHibernate references). This is necessary because your computer is not likely to have the same folder structure as my computer. :-)
3. Download Sample Solution
Download the sample solution and unzip to the location of your choice. The solution should work from any folder. If you have Microsoft Access, feel free to open the Sample.MDB database and look around. It's nothing special.
4. Open Solution and Fix References
NHibernate is not distributed with this sample. You will need to add references the NHibernate installation on your computer. The solution will probably show some warning icons next to the references.
Fix the references to NHibernate.dll, NHibernate.JetDriver.dll and log4net.dll. All three assemblies are located at <nhibernate-folder>/bin/net-2.0. For example, if you installed NHibernate to the default folder, the assemblies will be located at C:/Program Files/NHibernate/bin/net-2.0.
Example Results
The sample code will configure NHibernate to use Sample.mdb, then perform a few basic operations (e.g. saving a user, loading a user, and deleting a user) with all output sent to the console. In the future I will add more complex operations but hopefully this will get you started.
Source Code Comments
If the sample works but you're still unable to get Jet to work with your real application, try the following:
-
Make sure your mapping files are marked as embedded resources (if you're loading mappings from an assembly).
-
Make sure your field names are not reserved by the Jet database engine. For example, the word "Password" is reserved. You must enclose reserved words in square brackets in your mapping files. Look at User.xbm.xml for an example.
-
Make sure hibernate.cfg.xml is being copied to your output folder (if applicable). You can instruct Visual Studio to copy this file to your output folder when rebuilding.
-
Make sure your connection string has the correct path to your MDB file. You can use relative paths. For example, if your database is located two folders above your executable folder, you can use a path like "../../MyData.mdb".
-
Make sure you're using the correct namespaces in your mapping and configuration files.
And please note: Access is not designed for server or enterprise applications. It is a desktop database system. You can reach the author at dave@thoughtproject.com for feedback or questions about this sample.
External Links
NHibernate
http://www.nhibernate.org
This is the official URL for the excellent NHibernate open source O/R system.
NHibernate Quick Start Guide
http://www.hibernate.org/362.html
A good starting point for learning NHibernate.
David Givoni's Blog
http://david.givoni.com/blog/
His blog has a number of useful postings about NHibernate. This posting about reserved words in SQL saved me a lot of time.
SQL Reserved Words
http://sqlserver2000.databases.aspfaq.com/what-are-reserved-access-odbc-and-sql-server-keywords.html
Refer to this list if you get a syntax error with certain field names. Reserved field names can be enclosed in [square brackets], e.g.:
<property name="Password" column="[Password]" type="String" length="50"/>