让IBatisNet支持DataTable,和分页(support DataMapper-1.5.1)

在我们的实际框架中更多的地方需要得到SQL语句。复杂的SQL主要是查询.本文介绍了怎么让IBatisNet支持DataTable。

IBatisNet之获取和操作SQL语句
  一文中的方法不支持最新版本的IBatisNet。根据IBatisNet新版本改为:
ContractedBlock.gif ExpandedBlockStart.gif GetSql/GetDataTable #region GetSql/GetDataTable
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 得到参数化后的SQL
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static string GetSql(string tag, object paramObject)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IStatement statement 
= Mapper.Instance().GetMappedStatement(tag).Statement;
InBlock.gif
InBlock.gif            IMappedStatement mapStatement 
= Mapper.Instance().GetMappedStatement(tag);
InBlock.gif
InBlock.gif            IDalSession session 
= new SqlMapSession(Mapper.Instance());
InBlock.gif
InBlock.gif            RequestScope request 
= statement.Sql.GetRequestScope(mapStatement, paramObject, session);
InBlock.gif
InBlock.gif            
return request.PreparedStatement.PreparedSql;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected static IDbCommand GetDbCommand(string tag, object paramObject)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IStatement statement 
= Mapper.Instance().GetMappedStatement(tag).Statement;
InBlock.gif
InBlock.gif            IMappedStatement mapStatement 
= Mapper.Instance().GetMappedStatement(tag);
InBlock.gif
InBlock.gif            IDalSession session 
= new SqlMapSession(Mapper.Instance());
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
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public static DataTable GetDataTable(string tag, object paramObject)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            
bool isSessionLocal = false;
InBlock.gif            IDalSession session 
= Mapper.Instance().LocalSession;
InBlock.gif            
if (session == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session 
= new SqlMapSession(Mapper.Instance());
InBlock.gif                session.OpenConnection();
InBlock.gif                isSessionLocal 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                IDbCommand cmd 
= GetDbCommand(tag, paramObject);
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            
return ds.Tables[0];
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于分页控件使用
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tag">语句ID</param>
InBlock.gif        
/// <param name="paramObject">语句所需要的参数</param>
InBlock.gif        
/// <param name="PageSize">每页显示数目</param>
InBlock.gif        
/// <param name="curPage">当前页</param>
InBlock.gif        
/// <param name="recCount">记录总数</param>
ExpandedSubBlockEnd.gif        
/// <returns>得到的DataTable</returns>

InBlock.gif        public static DataTable GetDataTable(string tag, object paramObject, int PageSize, int curPage, out int recCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IDataReader dr 
= null;
InBlock.gif            
bool isSessionLocal = false;
InBlock.gif            
string sql = GetSql(tag, paramObject);
InBlock.gif            
string strCount = "select count(*) " + sql.Substring(sql.ToLower().IndexOf("from"));
InBlock.gif
InBlock.gif            IDalSession session 
= Mapper.Instance().LocalSession;
InBlock.gif            DataTable dt 
= new DataTable();
InBlock.gif            
if (session == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session 
= new SqlMapSession(Mapper.Instance());
InBlock.gif                session.OpenConnection();
InBlock.gif                isSessionLocal 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                IDbCommand cmdCount 
= GetDbCommand(tag, paramObject);
InBlock.gif                cmdCount.Connection 
= session.Connection;
InBlock.gif                cmdCount.CommandText 
= strCount;
InBlock.gif                
object count = cmdCount.ExecuteScalar();
InBlock.gif                recCount 
= Convert.ToInt32(count);
InBlock.gif
InBlock.gif                IDbCommand cmd 
= GetDbCommand(tag, paramObject);
InBlock.gif                cmd.Connection 
= session.Connection;
InBlock.gif                dr 
= cmd.ExecuteReader();
InBlock.gif
InBlock.gif                dt 
= Paging(dr, PageSize, curPage);
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            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 取回合适数量的数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="dataReader"></param>
InBlock.gif        
/// <param name="PageSize"></param>
InBlock.gif        
/// <param name="curPage"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected static DataTable Paging(IDataReader dataReader, int PageSize, int curPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt;
InBlock.gif            dt 
= new DataTable();
InBlock.gif            
int colCount = dataReader.FieldCount;
InBlock.gif            
for (int i = 0; i < colCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dt.Columns.Add(
new DataColumn(dataReader.GetName(i), dataReader.GetFieldType(i)));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// 读取数据。将DataReader中的数据读取到DataTable中
InBlock.gif

InBlock.gif            
object[] vald = new object[colCount];
InBlock.gif            
int iCount = 0// 临时记录变量
InBlock.gif
            while (dataReader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 当前记录在当前页记录范围内
InBlock.gif

InBlock.gif                
if (iCount >= PageSize * (curPage - 1&& iCount < PageSize * curPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
for (int i = 0; i < colCount; i++)
InBlock.gif                        vald[i] 
= dataReader.GetValue(i);
InBlock.gif
InBlock.gif                    dt.Rows.Add(vald);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (iCount > PageSize * curPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

InBlock.gif                iCount
++// 临时记录变量递增
ExpandedSubBlockEnd.gif
            }

InBlock.gif
InBlock.gif            
if (!dataReader.IsClosed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dataReader.Close();
InBlock.gif                dataReader.Dispose();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion



转载于:https://www.cnblogs.com/try/archive/2006/11/23/569770.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值