日期:2014-05-19  浏览次数:20904 次

100分求一个nhibernate链接sql数据库的例子 (急)
带事务和存储过程的

------解决方案--------------------
1、NhibernateService,封装了NhFactory,NHSession。可以很简单的增加其他的数据库。目前连接了两个MySql数据库,一个SqlServe
增加新的数据库:
添加一个类:简单的继承SessionFactory就可以了,如下面增加一个连接Ser的库:


class SerSessionFactory:SessionFactory
{
public static readonly SessionFactory _sessionFactory = new SerSessionFactory();
protected SerSessionFactory()
: base( "Ser.cfg.xml ")
{


}
}


添加配置文件:


<?xml version= "1.0 " encoding= "utf-8 " ?>
<hibernate-configuration xmlns= "urn:nhibernate-configuration-2.0 ">
<session-factory>
<property name= "connection.provider "> NHibernate.Connection.DriverConnectionProvider </property>
<property name= "connection.driver_class "> NHibernate.Driver.MySqlDataDriver </property>
<property name= "connection.connection_string "> Server=203.193.42.37;initial catalog=radius;uid=**;pwd=** </property>
<property name= "show_sql "> false </property>
<property name= "dialect "> NHibernate.Dialect.MySQLDialect </property>
<!-- <property name= "use_outer_join "> true </property>
<property name= "query.substitutions "> true 1, false 0, yes 'Y ', no 'N ' </property> -->

<mapping assembly= "LoginBase " />
</session-factory>
</hibernate-configuration>


然后就可以通过Repository操作数据库了


------解决方案--------------------
使用存储过程:
映射文件里:
<sql-query name= "GetCustomerOrderHistory ">
<return-scalar column= "ProductName " type= "String " />
<return-scalar column= "Total " type= "Int32 " />
exec CustOrderHist :CustomerID
</sql-query>
使用:
IQuery query = NHibernateSession.GetNamedQuery( "GetCustomerOrderHistory ")
.SetString( "CustomerID ", customerId)
.SetResultTransformer(
new NHibernate.Transform.AliasToBeanConstructorResultTransformer(
typeof (HistoricalOrderSummary).GetConstructors()[0]));

return query.List <HistoricalOrderSummary> () as List <HistoricalOrderSummary> ;
------解决方案--------------------
学习,帮顶
------解决方案--------------------
用Castle把,这玩意把nhibernate封装了一次,更容易使用了