NHibernate执行SQL语句

本文介绍如何使用NHibernate作为数据访问层执行SQL语句的方法。通过创建SessionManager类管理会话,利用ADO.NET实现自定义SQL的执行。适用于需要在NHibernate环境中运行特定SQL的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      如果需要在使用NHibernate作为数据访问层的时候执行SQL语句,可以参考以下方法:

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1using System.Web;
 2using NHibernate;
 3using NHibernate.Cfg;
 4using NHibernate.Engine;
 5
 6namespace NHibernateStudy.DAL
 7ExpandedBlockStart.gifContractedBlock.gif{    
 8    public sealed class SessionManager
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    {
10        private const string CurrentSessionKey = "nhibernate.current_session";
11        private static readonly ISessionFactory sessionFactory;
12
13        static SessionManager()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        {            
15            //Configuration cf = new Configuration();
16            //cf.AddAssembly("Example.Model");
17            //sessionFactory = cf.Configure().BuildSessionFactory();
18            sessionFactory = new Configuration().Configure().BuildSessionFactory();
19        }

20
21        public static ISession GetCurrentSession()
22ExpandedSubBlockStart.gifContractedSubBlock.gif        {
23            HttpContext context = HttpContext.Current;
24            ISession currentSession = context.Items[CurrentSessionKey] as ISession;
25
26            if (currentSession == null)
27ExpandedSubBlockStart.gifContractedSubBlock.gif            {
28                currentSession = sessionFactory.OpenSession();
29                context.Items[CurrentSessionKey] = currentSession;
30            }

31
32            return currentSession;
33        }

34
35        public static void CloseSession()
36ExpandedSubBlockStart.gifContractedSubBlock.gif        {
37            HttpContext context = HttpContext.Current;
38            ISession currentSession = context.Items[CurrentSessionKey] as ISession;
39
40            if (currentSession == null)
41ExpandedSubBlockStart.gifContractedSubBlock.gif            {
42                // No current session
43                return;
44            }

45
46            currentSession.Close();
47            context.Items.Remove(CurrentSessionKey);
48        }

49
50        public static void CloseSessionFactory()
51ExpandedSubBlockStart.gifContractedSubBlock.gif        {
52            if (sessionFactory != null)
53ExpandedSubBlockStart.gifContractedSubBlock.gif            {
54                sessionFactory.Close();
55            }

56        }

57
58        public static ISessionFactoryImplementor GetISessionFactoryImplementor()
59ExpandedSubBlockStart.gifContractedSubBlock.gif        {
60            if (sessionFactory != null)
61ExpandedSubBlockStart.gifContractedSubBlock.gif            {
62                return (ISessionFactoryImplementor)sessionFactory;
63            }

64            else
65ExpandedSubBlockStart.gifContractedSubBlock.gif            {
66                return null;
67            }

68        }

69    }

70
71}

72

客户端调用:

 

ContractedBlock.gifExpandedBlockStart.gifCode
protected void Test
        {
            ISessionFactoryImplementor sfi 
= SessionManager.GetISessionFactoryImplementor();
            IDbConnection conn 
= sfi.OpenConnection();
            
            IDbTransaction tran 
= conn.BeginTransaction();
            
try
            {
                IDbCommand com 
= conn.CreateCommand();
                
//以下可以随意调用ADO.NET了
                tran.Commit();

            }
            
catch
            {
                tran.Rollback();
            }     
           
        }

 

转载于:https://www.cnblogs.com/millen/archive/2009/11/16/1603733.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值