NHibernate1.02使用MsAccess2000Dialect 提供对 Access 数据库的支持
NHibernateContrib 提供的JetDriver访问Access数据库简直是噩梦连连。插入更新含有日期类型的都回出现异常。在网上找到用 MsAccess2000Dialect 访问Access数据库的方法。1.02版本的NHibernate 或者NHibernateContrib本身没有包含MsAccess2000Dialect 在这里找到了它的下载地址 http://wiki.nhibernate.org/pages/viewpageattachments.action?pageId=345
包含两个文件:msaccess2000joinfragment.cs 和 msaccess2000dialect.cs 。
打开NHibernate的项目后把两个文件放到这里
然后重新编译NHibernate的项目就可以了。但是下载下来的文件有几个地方要修改一下。
- MsAccess2000Dialect.cs文件的这个地方
protected override char CloseQuote的访问修饰符应该改为public。
{
get { return ']'; }
}
/// <summary></summary>
protected override char OpenQuote
{
get { return '['; }
} - MsAccess2000Dialect.cs的构造函数
public MsAccess2000Dialect() : base()中的Register方法应该全部改为RegisterColumnType方法.
{
Register( DbType.AnsiStringFixedLength, "CHAR(255)" );
Register( DbType.AnsiStringFixedLength, 255,"CHAR($1)" );
Register( DbType.AnsiStringFixedLength, 8000, "LONGTEXT" );
Register( DbType.AnsiString, "VARCHAR(255)" );
Register( DbType.AnsiString, 255,"VARCHAR($1)" );
Register( DbType.AnsiString, 2147483647, "LONGTEXT" );
Register( DbType.Binary, "IMAGE" );
Register( DbType.Boolean, "BIT" );
Register( DbType.Byte, "TINYINT" );
Register( DbType.Currency, "MONEY" );
Register( DbType.Date, "DATETIME" );
Register( DbType.DateTime, "DATETIME" );
Register( DbType.Decimal, "NUMERIC" );
Register( DbType.Double, "DOUBLE" ); //synonym for FLOAT(53)
Register( DbType.Guid, "UNIQUEIDENTIFIER" );
Register( DbType.Int16, "INT" );
Register( DbType.Int32, "LONG" );
Register( DbType.Int64, "NUMERIC" );
Register( DbType.Single, "REAL" ); //synonym for FLOAT(24)
Register( DbType.StringFixedLength, "CHAR(255)" );
Register( DbType.StringFixedLength, 255,"CHAR($1)" );
Register( DbType.StringFixedLength, 4000, "LONGTEXT" );
Register( DbType.String, "VARCHAR(255)" );
Register( DbType.String, 255,"VARCHAR($1)" );
Register( DbType.String, 1073741823, "LONGTEXT" );
Register( DbType.Time, "DATETIME" );
DefaultProperties[ Environment.OuterJoin ] = "true";
DefaultProperties[ Environment.ConnectionDriver ] = "NHibernate.Driver.OleDbDriver";
DefaultProperties[ Environment.PrepareSql ] = "false";
} - 构造函数中
DefaultProperties[ Environment.UseOuterJoin ] = "true";的定义改为 Environment.UseOuterJoin。
修改完毕后使用这个连接配置文件就可以了。
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<nhibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsAccess2000Dialect"/>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.OleDbDriver"/>
<add key="hibernate.connection.connection_string" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Access数据库的路径和名称"/>
</nhibernate>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<nhibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsAccess2000Dialect"/>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.OleDbDriver"/>
<add key="hibernate.connection.connection_string" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Access数据库的路径和名称"/>
</nhibernate>
懒得重新编译的朋友下可以下这个已经重新编译过的NHibernate的单项目 /Files/Bruce_H21/NHibernate.rar
不知道有没有人用SQL Server 7 dialect来访问Access数据库?
如需转载请注明出处