在Nhibernate中执行sql语句

在Nhibernate中执行sql语句,如果不将查询后的结果转换成对象类型,在绑定datagrid等数据源的时候就会出现问题,具体做法如下:
执行sql语句函数(query表示sql查询语句,tablename也可以理解为表名,但实际的作用是映射到相对应的实体类):
   
 1 None.gif private  IList ExecuteSQL(  string  query, string  tablename) 
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            IList result = new ArrayList();
 4InBlock.gif            ISessionFactoryImplementor s = (ISessionFactoryImplementor)_cfg.BuildSessionFactory();
 5InBlock.gif            IDbCommand cmd = s.ConnectionProvider.Driver.CreateCommand();
 6InBlock.gif            cmd.CommandText = query;
 7InBlock.gif            IDbConnection conn = s.OpenConnection();
 8InBlock.gif            try 
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
10InBlock.gif                cmd.Connection = conn;
11InBlock.gif                IDataReader rs = cmd.ExecuteReader();
12InBlock.gif                int j = 0 ;
13InBlock.gif                while ( rs.Read() ) 
14ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
15InBlock.gif                    int fieldCount = rs.FieldCount;
16InBlock.gif                    object[] values = new Object[ fieldCount ];
17InBlock.gif                    for ( int i = 0; i < fieldCount; i ++ )
18InBlock.gif                        values[i] = rs.GetValue(i);
19InBlock.gif                    chooseEntity(result,tablename,values);
20ExpandedSubBlockEnd.gif                }

21ExpandedSubBlockEnd.gif            }

22InBlock.gif            finally 
23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
24InBlock.gif                s.CloseConnection(conn);
25ExpandedSubBlockEnd.gif            }

26InBlock.gif            return result;
27ExpandedBlockEnd.gif        }

在以上的代码中,函数chooseEntity就是对查询的结的转换,如下:
    
 1 None.gif private   void  chooseEntity(IList result, string  tablename, object [] values)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            switch(tablename)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 5InBlock.gif                case "tbl_b_AcceptConiteminfo":
 6InBlock.gif                    AcceptConIteminfo accConitem = new AcceptConIteminfo(values);
 7InBlock.gif                    result.Add( accConitem );
 8InBlock.gif                    break;
 9ExpandedSubBlockEnd.gif            }

10ExpandedBlockEnd.gif        }

在实际类的构造函数中,将数组values的值赋值到实体类中,如下:
    
ContractedBlock.gif ExpandedBlockStart.gif 实体类实例
 1None.gifpublic class AcceptConIteminfo
 2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
 3InBlock.gif
 4InBlock.gif        private string _OrigItemId = ""
 5InBlock.gif        private string _ItemName = "";  
 6InBlock.gif        private string _ItemLoc = "";   
 7InBlock.gif        private string _IsnewItem = ""
 8InBlock.gif        public AcceptConIteminfo(object[] values)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10InBlock.gif            //
11InBlock.gif            // TODO: 在此处添加构造函数逻辑
12InBlock.gif            //
13InBlock.gif            _OrigItemId = values[0].ToString() ;
14InBlock.gif            _ItemName = values[1].ToString() ;
15InBlock.gif            _ItemLoc = values[2].ToString() ;
16InBlock.gif            _IsnewItem = values[3].ToString() ;
17ExpandedSubBlockEnd.gif        }

18InBlock.gif
19InBlock.gif
20InBlock.gif        public string OrigItemId
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return _OrigItemId;}
23ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{_OrigItemId= value ;}
24ExpandedSubBlockEnd.gif        }

25InBlock.gif        
26InBlock.gif        public string ItemName
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return _ItemName;}
29ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{_ItemName= value ;}
30ExpandedSubBlockEnd.gif        }

31InBlock.gif
32InBlock.gif        public string ItemLoc
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return _ItemLoc;}
35ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{_ItemLoc= value ;}
36ExpandedSubBlockEnd.gif        }

37InBlock.gif
38InBlock.gif        public string IsnewItem
39ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
40ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return _IsnewItem;}
41ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{_IsnewItem= value ;}
42ExpandedSubBlockEnd.gif        }

43ExpandedBlockEnd.gif    }
ok,经过以上的处理,可以直接用来绑定数据源,当然,这样处理有不好的地方,只不过也给出了一个解决问题的方法。
 

转载于:https://www.cnblogs.com/oldhorse/archive/2006/07/08/445958.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值