首页 > 代码库 > NHibernate支持的数据库 NHibernate连接配置 hibernate.cfg.xml中的配置

NHibernate支持的数据库 NHibernate连接配置 hibernate.cfg.xml中的配置

使用下列数据库时hibernate.cfg.xml中的配置

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> 

SQL2000的话更改方言为 NHibernate.Dialect.MsSql2000Dialect.

有个问题注意一下:SQL Server 如果不在SELECT中指选择列,则有时ORDER BY子句会忽略这些特殊的列,而这种行为在标准SQL中有效的,对于举例来说,象这样查询是不保证正确的: from Person p order by p.Company.Name

Oracle配置如下:

Oracle 9i 和 10g 都支持引用Microsoft driver (System.Data.OracleClient) 和 Oracle driver (Oracle.Data.OracleClient).

有个问题注意一下:当字符串参数中的长度在20000-4000时,微软驱动不能正确的处理长字符,另外Oracle不能接受空字符串,你可以使用null来代替,在NhibernateContrib包的Nullables.Nhibernate类中的IUserType实现转换的问题

Microsoft Access配置如下:

Microsoft Access有自己的dialect和driver,在NHibernateContrib包里的NHibernate.JetDriver.dll中.


<?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

若要使用Firebird首先安装最新的火鸟.NET数据提供程序安装在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> 

这里的version取决于你安装的版本。
<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> 

2.0.1所有Firebird嵌入式函数和ib_udf2.sql用户定义函数均已包含在方言里了,这些函数可以直接在HQL使用。

PostgreSQL

建议使用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>


MySQL

<?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> 

有个问题注意一下:MySQL有一个特殊的功能,允许在日期字段无效的日期,并没有NULL列,默认值是0000-00-00。当MySQL的连接器遇到这样一个日期,它要么抛出一个异常或返回非标准MySqlDateTime对象(一个连接字符串参数而定)会抛出一个异常时,转换为DateTime本身。
可能的变通办法:避免零日期;修改连接器(NET源代码),以随机或零日期DateTime.MinValue;创建MySqlDateTime用户类型(没有已知的实现目前);

SQLite

从http://sourceforge.net/projects/adodotnetsqlite下载using ADO.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.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>

NHibernate支持的数据库 NHibernate连接配置 hibernate.cfg.xml中的配置