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

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

 

private IDbCommand GetDbCommand(string statementName, object paramObject)
        
{
            IStatement statement 
= sqlMap.GetMappedStatement(statementName).Statement;

            IMappedStatement mapStatement 
= sqlMap.GetMappedStatement(statementName);

            IDalSession session 
= new SqlMapSession(sqlMap);

            
if (sqlMap.LocalSession != null)
            
{
                session 
= sqlMap.LocalSession;
            }

            
else
            
{
                session 
= sqlMap.OpenConnection();
            }


            RequestScope request 
= statement.Sql.GetRequestScope(mapStatement, paramObject, session);

            mapStatement.PreparedCommand.Create(request, session, statement, paramObject);

            
return request.IDbCommand;

        }



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

        protected DataTable ExecuteQueryForDataTable(string statementName, object paramObject)
        
{
            DataSet ds 
= new DataSet();
            
bool isSessionLocal = false;
            IDalSession session 
= sqlMap.LocalSession;
            
if (session == null)
            
{
                session 
= new SqlMapSession(sqlMap);
                session.OpenConnection();
                isSessionLocal 
= true;
            }


            IDbCommand cmd 
= GetDbCommand(statementName, paramObject);

            
try
            
{
                cmd.Connection 
= session.Connection;
                IDbDataAdapter adapter 
= session.CreateDataAdapter(cmd);
                adapter.Fill(ds);
            }

            
finally
            
{
                
if (isSessionLocal)
                
{
                    session.CloseConnection();
                }

            }


            
return ds.Tables[0];

        }


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

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

        protected DataTable ExecuteQueryForDataTable(string statementName, object paramObject, out Hashtable htOutPutParameter)
        
{
            DataSet ds 
= new DataSet();
            
bool isSessionLocal = false;
            IDalSession session 
= sqlMap.LocalSession;
            
if (session == null)
            
{
                session 
= new SqlMapSession(sqlMap);
                session.OpenConnection();
                isSessionLocal 
= true;
            }


            IDbCommand cmd 
= GetDbCommand(statementName, paramObject);

            
try
            
{
                cmd.Connection 
= session.Connection;
                IDbDataAdapter adapter 
= session.CreateDataAdapter(cmd);
                adapter.Fill(ds);
            }

            
finally
            
{
                
if (isSessionLocal)
                
{
                    session.CloseConnection();
                }

            }


            
foreach (IDataParameter parameter in cmd.Parameters)
            
{
                
if (parameter.Direction == ParameterDirection.Output)
                
{
                    htOutPutParameter[parameter.ParameterName] 
= parameter.Value;
                }

            }


            
return ds.Tables[0];

        }

posted on 2007-02-05 19:04  Daniel Pang  阅读(3742)  评论(4编辑  收藏  举报

导航