NHibernate小结之二

这里给出所有常用的数据库配置文件,这样不用再到官网找了,速度比较慢,国外的网站啰。

 

来源于官网的内容,这里只是转帖一下。

 

Microsoft SQL Server 2005/2000

  1. <?xml version="1.0" ?>
  2. <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
  3.     <session-factory>
  4.         <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  5.         <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
  6.         <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
  7.         <property name="connection.connection_string">Server=(local);Initial Catalog=dbname;User Id=user;Password=********</property>
  8.     </session-factory>
  9. </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这个包,里面也有,但版本就不知道会不会比较统一了,我感觉还是前者比较保险。

  1. <?xml version="1.0" ?>
  2. <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  3.     <session-factory>
  4.         <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  5.         <property name="dialect">NHibernate.JetDriver.JetDialect, NHibernate.JetDriver</property>
  6.         <property name="connection.driver_class">NHibernate.JetDriver.JetDriver, NHibernate.JetDriver</property>
  7.         <property name="connection.connection_string">Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourDatabaseFilePathHere.mdb</property>
  8.     </session-factory>
  9. </hibernate-configuration>

======================================================

 

Firebird

 

从1.5.3版开始支持,强烈推荐2.0.1版。先要安装最新的官方.net数据提供器(Firebird .NET Data Provider)。如果安装到GAC(标准安装的话),要加入下面的小节到程序配置文件中(App.config或者Web.config)

  1. <runtime>
  2.     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  3.         <qualifyAssembly partialName="FirebirdSql.Data.FirebirdClient"
  4.             fullName="FirebirdSql.Data.FirebirdClient, Version=2.0.1.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c" />
  5.     </assemblyBinding>
  6. </runtime>

fullname这里要根据实际版本进行修改。下面给出一般配置文件

  1. <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  2.     <session-factory name="NHibernate.Test">
  3.         <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  4.         <property name="connection.driver_class">NHibernate.Driver.FirebirdClientDriver</property>
  5.         <property name="connection.isolation">ReadCommitted</property>
  6.         <property name="connection.connection_string">
  7.             Server=localhost;
  8.             Database=C:\nhibernate.fdb;
  9.             User=SYSDBA;Password=masterkey
  10.         </property>
  11.         <property name="show_sql">false</property>
  12.         <property name="dialect">NHibernate.Dialect.FirebirdDialect</property>
  13.         <property name="use_outer_join">true</property>
  14.         <property name="command_timeout">444</property>
  15.         <property name="query.substitutions">true 1, false 0, yes 1, no 0</property>
  16.     </session-factory>
  17. </hibernate-configuration>

=================================================

PostgreSQL

 

7.4版开始支持。

配置文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  3.     <session-factory name="NHibernate.Test">
  4.         <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  5.         <property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
  6.         <property name="connection.connection_string">
  7.             Server=localhost;initial catalog=nhibernate;User ID=nhibernate;Password=********;
  8.         </property>
  9.         <property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
  10.     </session-factory>
  11. </hibernate-configuration>

=======================================================

 

DB2 UDB

 

好像支持的非常好,具体见http://nhibernate.sourceforge.net/forum/viewtopic.php?t=73。下面的配置用的stinger版的db2的odbc驱动,配置如下:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <hibernate-configuration xmlns="urn:nhibernate-configuration-2.0" >

  3.     <session-factory name="session">

  4.         <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  5.         <property name="connection.driver_class">NHibernate.Driver.OdbcDriver</property>
  6.         <property name="connection.connection_string">driver={IBM DB2 ODBC DRIVER};Database=db;hosthostname=host;portport=port;protocol=TCPIP; uiduid=uid; pwdpwd=pwd</property>
  7.         <property name="show_sql">true</property>
  8.         <property name="dialect">NHibernate.Dialect.DB2Dialect</property>
  9.         <property name="use_outer_join">true</property>

  10.         <mapping resource="..." />

  11.     
  12.     </session-factory>

  13. </hibernate-configuration>

=======================================================

 

MySQL

 

所有版本的都能用,除了零日期问题(mysql的DATE类型可以让错误数据进来,特别是用0000-00-00作为默认的DATE非空数据。而MySQL的连接器发现这个情况会抛出异常。

驱动当然去MySQL的官网下.net的驱动。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  3.     <session-factory name="NHibernate.Test">
  4.         <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  5.         <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
  6.         <property name="connection.connection_string">
  7.             Database=test;Data Source=someip;User Id=blah;Password=blah
  8.         </property>
  9.         <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
  10.     </session-factory>
  11. </hibernate-configuration>

=======================================================

 

SQLite

 

推荐 version3版和http://sourceforge.net/projects/adodotnetsqlite提示器一起用。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  3.     <session-factory name="NHibernate.Test">
  4.         <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  5.         <property name="connection.driver_class">NHibernate.Driver.SQLiteDriver</property>
  6.         <property name="connection.connection_string">
  7.             Data Source=nhibernate.db;Version=3
  8.         </property>
  9.         <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
  10.         <property name="query.substitutions">true=1;false=0</property>
  11.     </session-factory>
  12. </hibernate-configuration>

posted @ 2011-01-16 01:01  悲伤的第七音  Views(277)  Comments(0Edit  收藏  举报