NHibernate with Microsoft Access (Jet) Sample

This sample solution demonstrates NHibernate with Microsoft Access (Jet).  The code demonstrates:

The sample is distributed as a Visual Studio 2005 solution with C#.  The sample requires NHibernate 1.2.0.CR1 (distributed separately).

Download

Download NHibernateWithAccess.zip (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.

References Screenshot

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.

Output Screenshot

Source Code Comments

If the sample works but you're still unable to get Jet to work with your real application, try the following:

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"/>