[IBatisNet]关于返回DataTable的一点问题

      对于IBatisnet执行查询返回DataTable,网上有很多代码,下面简单列出一下:

 

None.gif private  IDbCommand GetDbCommand( string  statementName,  object  paramObject)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            IStatement statement 
= sqlMap.GetMappedStatement(statementName).Statement;
InBlock.gif
InBlock.gif            IMappedStatement mapStatement 
= sqlMap.GetMappedStatement(statementName);
InBlock.gif
InBlock.gif            IDalSession session 
= new SqlMapSession(sqlMap);
InBlock.gif
InBlock.gif            
if (sqlMap.LocalSession != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session 
= sqlMap.LocalSession;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session 
= sqlMap.OpenConnection();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            RequestScope request 
= statement.Sql.GetRequestScope(mapStatement, paramObject, session);
InBlock.gif
InBlock.gif            mapStatement.PreparedCommand.Create(request, session, statement, paramObject);
InBlock.gif
InBlock.gif            
return request.IDbCommand;
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 通用的以DataTable的方式得到Select的结果(xml文件中参数要使用$标记的占位参数)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="statementName">语句ID</param>
InBlock.gif        
/// <param name="paramObject">语句所需要的参数</param>
ExpandedBlockEnd.gif        
/// <returns>得到的DataTable</returns>

None.gif          protected  DataTable ExecuteQueryForDataTable( string  statementName,  object  paramObject)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            
bool isSessionLocal = false;
InBlock.gif            IDalSession session 
= sqlMap.LocalSession;
InBlock.gif            
if (session == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session 
= new SqlMapSession(sqlMap);
InBlock.gif                session.OpenConnection();
InBlock.gif                isSessionLocal 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            IDbCommand cmd 
= GetDbCommand(statementName, paramObject);
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cmd.Connection 
= session.Connection;
InBlock.gif                IDbDataAdapter adapter 
= session.CreateDataAdapter(cmd);
InBlock.gif                adapter.Fill(ds);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (isSessionLocal)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    session.CloseConnection();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return ds.Tables[0];
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif


理论上来说是没任何问题的,但前几天我做了有Output参数的存储过程来返回DataTable,如果用上面的ExecuteQueryForDataTable方法就会获得不到Output的参数,因为它在返回值的时候没有用到IBatisNet内部的方法了,所以上面的方法严谨的写法应该改成下面这样:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif        
/// 通用的以DataTable的方式得到Select的结果(xml文件中参数要使用$标记的占位参数)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="statementName">语句ID</param>
InBlock.gif        
/// <param name="paramObject">语句所需要的参数</param>
InBlock.gif    
/// <param name="htOutPutParameter)">Output参数值哈希表</param>
ExpandedBlockEnd.gif        
/// <returns>得到的DataTable</returns>

None.gif          protected  DataTable ExecuteQueryForDataTable( string  statementName,  object  paramObject,  out  Hashtable htOutPutParameter)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            
bool isSessionLocal = false;
InBlock.gif            IDalSession session 
= sqlMap.LocalSession;
InBlock.gif            
if (session == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session 
= new SqlMapSession(sqlMap);
InBlock.gif                session.OpenConnection();
InBlock.gif                isSessionLocal 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            IDbCommand cmd 
= GetDbCommand(statementName, paramObject);
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cmd.Connection 
= session.Connection;
InBlock.gif                IDbDataAdapter adapter 
= session.CreateDataAdapter(cmd);
InBlock.gif                adapter.Fill(ds);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (isSessionLocal)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    session.CloseConnection();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
foreach (IDataParameter parameter in cmd.Parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (parameter.Direction == ParameterDirection.Output)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    htOutPutParameter[parameter.ParameterName] 
= parameter.Value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return ds.Tables[0];
InBlock.gif
ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值