NHibernate小结之二
来源于官网的内容,这里只是转帖一下。
Microsoft SQL Server 2005/2000
- <?xml version="1.0" ?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory>
- <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
- <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
- <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
- <property name="connection.connection_string">Server=(local);Initial Catalog=dbname;User Id=user;Password=********</property>
- </session-factory>
- </hibernate-configuration>
如果用2000的话,对应的dialect 改为NHibernate.Dialect.MsSql2000Dialect即可。
====================================================
Oracle
支持9i和10g两个版本。驱动用微软的System.Data.OracleClient或者官方的Oracle.Data.OracleClient都可以。
====================================================
Access
驱动在Nhibernate.JetDriver.dll里面,这个在NHibernate1.21版的非MSI安装包的下载中有,就是一 个zip包的,里面有。另外如果单独下NHibernateContrib这个包,里面也有,但版本就不知道会不会比较统一了,我感觉还是前者比较保险。
- <?xml version="1.0" ?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory>
- <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
- <property name="dialect">NHibernate.JetDriver.JetDialect, NHibernate.JetDriver</property>
- <property name="connection.driver_class">NHibernate.JetDriver.JetDriver, NHibernate.JetDriver</property>
- <property name="connection.connection_string">Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourDatabaseFilePathHere.mdb</property>
- </session-factory>
- </hibernate-configuration>
======================================================
Firebird
从1.5.3版开始支持,强烈推荐2.0.1版。先要安装最新的官方.net数据提供器(Firebird .NET Data Provider)。如果安装到GAC(标准安装的话),要加入下面的小节到程序配置文件中(App.config或者Web.config)
- <runtime>
- <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
- <qualifyAssembly partialName="FirebirdSql.Data.FirebirdClient"
- fullName="FirebirdSql.Data.FirebirdClient, Version=2.0.1.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c" />
- </assemblyBinding>
- </runtime>
fullname这里要根据实际版本进行修改。下面给出一般配置文件
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory name="NHibernate.Test">
- <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
- <property name="connection.driver_class">NHibernate.Driver.FirebirdClientDriver</property>
- <property name="connection.isolation">ReadCommitted</property>
- <property name="connection.connection_string">
- Server=localhost;
- Database=C:\nhibernate.fdb;
- User=SYSDBA;Password=masterkey
- </property>
- <property name="show_sql">false</property>
- <property name="dialect">NHibernate.Dialect.FirebirdDialect</property>
- <property name="use_outer_join">true</property>
- <property name="command_timeout">444</property>
- <property name="query.substitutions">true 1, false 0, yes 1, no 0</property>
- </session-factory>
- </hibernate-configuration>
=================================================
PostgreSQL
7.4版开始支持。
配置文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory name="NHibernate.Test">
- <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
- <property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
- <property name="connection.connection_string">
- Server=localhost;initial catalog=nhibernate;User ID=nhibernate;Password=********;
- </property>
- <property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
- </session-factory>
- </hibernate-configuration>
=======================================================
DB2 UDB
好像支持的非常好,具体见http://nhibernate.sourceforge.net/forum/viewtopic.php?t=73。下面的配置用的stinger版的db2的odbc驱动,配置如下:
- <?xml version="1.0" encoding="utf-8" ?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.0" >
- <session-factory name="session">
- <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
- <property name="connection.driver_class">NHibernate.Driver.OdbcDriver</property>
- <property name="connection.connection_string">driver={IBM DB2 ODBC DRIVER};Database=db;hosthostname=host;portport=port;protocol=TCPIP; uiduid=uid; pwdpwd=pwd</property>
- <property name="show_sql">true</property>
- <property name="dialect">NHibernate.Dialect.DB2Dialect</property>
- <property name="use_outer_join">true</property>
- <mapping resource="..." />
- </session-factory>
- </hibernate-configuration>
=======================================================
MySQL
所有版本的都能用,除了零日期问题(mysql的DATE类型可以让错误数据进来,特别是用0000-00-00作为默认的DATE非空数据。而MySQL的连接器发现这个情况会抛出异常。
驱动当然去MySQL的官网下.net的驱动。
- <?xml version="1.0" encoding="utf-8"?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory name="NHibernate.Test">
- <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
- <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
- <property name="connection.connection_string">
- Database=test;Data Source=someip;User Id=blah;Password=blah
- </property>
- <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
- </session-factory>
- </hibernate-configuration>
=======================================================
SQLite
推荐 version3版和http://sourceforge.net/projects/adodotnetsqlite提示器一起用。
- <?xml version="1.0" encoding="utf-8"?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory name="NHibernate.Test">
- <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
- <property name="connection.driver_class">NHibernate.Driver.SQLiteDriver</property>
- <property name="connection.connection_string">
- Data Source=nhibernate.db;Version=3
- </property>
- <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
- <property name="query.substitutions">true=1;false=0</property>
- </session-factory>
- </hibernate-configuration>