/// <summary> /// /// </summary> /// <param name="index"></param> /// <param name="localPageSize"></param> /// <returns></returns> private IList GetList(int index, int localPageSize) { bool isSessionLocal = false; ISqlMapSession session = _mappedStatement.SqlMap.LocalSession; if (session == null) { session = new SqlMapSession(_mappedStatement.SqlMap); session.OpenConnection(); isSessionLocal = true; } IList list = null; try { list = _mappedStatement.ExecuteQueryForList(session, _parameterObject, (index) * _pageSize, localPageSize); } catch { throw; } finally { if (isSessionLocal) { session.CloseConnection(); } } return(list); }
private IList GetList(int index, int localPageSize) { bool flag = false; ISqlMapSession localSession = this._mappedStatement.SqlMap.LocalSession; if (localSession == null) { localSession = new SqlMapSession(this._mappedStatement.SqlMap); localSession.OpenConnection(); flag = true; } IList list = null; try { list = this._mappedStatement.ExecuteQueryForList(localSession, this._parameterObject, index * this._pageSize, localPageSize); } catch { throw; } finally { if (flag) { localSession.CloseConnection(); } } return(list); }
public void TestMappedStatementQueryWithReadWriteCacheWithSession() { IMappedStatement statement = sqlMap.GetMappedStatement("GetRWNSCachedAccountsViaResultMap"); ISqlMapSession session = new SqlMapSession(sqlMap); session.OpenConnection(); int firstId = 0; int secondId = 0; try { // execute the statement twice; the second call should result in a cache hit IList list = statement.ExecuteQueryForList(session, null); firstId = HashCodeProvider.GetIdentityHashCode(list); list = statement.ExecuteQueryForList(session, null); secondId = HashCodeProvider.GetIdentityHashCode(list); } finally { session.CloseConnection(); } Assert.AreEqual(firstId, secondId); }
/// <summary> /// 通用的以DataTable的方式得到Select的结果(xml文件中参数要使用$标记的占位参数,注:不支持#变量替换) /// </summary> /// <param name="statementName">语句ID</param> /// <param name="paramObject">语句所需要的参数</param> /// <param name="htOutPutParameter">Output参数值哈希表</param> /// <returns>得到的DataTable</returns> public static DataTable ExecuteQueryForDataTable(string statementName, object paramObject, out Hashtable htOutPutParameter) { ISqlMapper sqlMap = Mapper(); DataSet ds = new DataSet(); bool isSessionLocal = false; IDalSession session = sqlMap.LocalSession; if (session == null) { session = new SqlMapSession(sqlMap); session.OpenConnection(); isSessionLocal = true; } IDbCommand cmd = GetCommand(statementName, paramObject); cmd.CommandTimeout = 0; try { cmd.Connection = session.Connection; IDbDataAdapter adapter = session.CreateDataAdapter(cmd); adapter.Fill(ds); } catch (Exception e) { throw new DataMapperException("Error executing query '" + statementName + "' for object. Cause: " + e.Message, e); } finally { if (isSessionLocal) { session.CloseConnection(); } } htOutPutParameter = new Hashtable(); foreach (IDataParameter parameter in cmd.Parameters) { if (parameter.Direction == ParameterDirection.Output) { htOutPutParameter[parameter.ParameterName] = parameter.Value; } } return(ds.Tables[0]); }
/// <summary> /// 用的以DataSet的方式得到Select的结果(xml文件中参数要使用$标记的占位参数. 注:不支持#变量替换) /// </summary> /// <param name="statementName"></param> /// <param name="paramObject"></param> /// <returns></returns> public static DataSet ExecuteQueryForDataSet(string statementName, object paramObject) { ISqlMapper sqlMap = Mapper(); DataSet ds = new DataSet(); bool isSessionLocal = false; IDalSession session = sqlMap.LocalSession; if (session == null) { session = new SqlMapSession(sqlMap); session.OpenConnection(); isSessionLocal = true; } IDbCommand cmd = GetCommand(statementName, paramObject); cmd.CommandTimeout = 0; try { cmd.Connection = session.Connection; IDbDataAdapter adapter = session.CreateDataAdapter(cmd); adapter.Fill(ds); } catch (Exception e) { throw new DataMapperException("Error executing query '" + statementName + "' for object. Cause: " + e.Message, e); } finally { if (isSessionLocal) { session.CloseConnection(); } } return(ds); }
/// <summary> /// 用于分页控件使用 /// </summary> /// <param name="tag">语句ID</param> /// <param name="paramObject">语句所需要的参数</param> /// <param name="PageSize">每页显示数目</param> /// <param name="curPage">当前页</param> /// <param name="recCount">记录总数</param> /// <returns>得到的DataTable</returns> public static DataTable QueryForDataTable(string tag, object paramObject, int PageSize, int curPage, out int recCount) { IDataReader dr = null; bool isSessionLocal = false; string sql = QueryForSql(tag, paramObject); string strCount = "select count(*) " + sql.Substring(sql.ToLower().IndexOf("from")); IDalSession session = SqlMap.LocalSession; DataTable dt = new DataTable(); if (session == null) { session = new SqlMapSession(SqlMap); session.OpenConnection(); isSessionLocal = true; } try { IDbCommand cmdCount = GetDbCommand(tag, paramObject); cmdCount.Connection = session.Connection; cmdCount.CommandText = strCount; object count = cmdCount.ExecuteScalar(); recCount = Convert.ToInt32(count); IDbCommand cmd = GetDbCommand(tag, paramObject); cmd.Connection = session.Connection; dr = cmd.ExecuteReader(); dt = QueryForPaging(dr, PageSize, curPage); } finally { if (isSessionLocal) { session.CloseConnection(); } } return(dt); }
public System.Data.DataTable QueryForDataTable(string statementName, StrObjectDict paramObject, StrObjectDict dictParam, System.Collections.Generic.IDictionary <string, System.Data.ParameterDirection> dictParamDirection, out System.Collections.Hashtable htOutPutParameter) { ISqlMapper sqlMap = this.GetSqlMap(); System.Data.DataSet dataSet = new System.Data.DataSet(); bool flag = false; ISqlMapSession sqlMapSession = sqlMap.LocalSession; if (sqlMapSession == null) { sqlMapSession = new SqlMapSession(sqlMap); sqlMapSession.OpenConnection(); flag = true; } System.Data.IDbCommand dbCommand = this.GetDbCommand(sqlMap, statementName, paramObject, dictParam, dictParamDirection, System.Data.CommandType.StoredProcedure); try { dbCommand.Connection = sqlMapSession.Connection; System.Data.IDbDataAdapter dbDataAdapter = sqlMapSession.CreateDataAdapter(dbCommand); dbDataAdapter.Fill(dataSet); } finally { if (flag) { sqlMapSession.CloseConnection(); } } htOutPutParameter = new System.Collections.Hashtable(); foreach (System.Data.IDataParameter dataParameter in dbCommand.Parameters) { if (dataParameter.Direction == System.Data.ParameterDirection.Output) { htOutPutParameter[dataParameter.ParameterName] = dataParameter.Value; } } return(dataSet.Tables[0]); }
public void Run() { try { IMappedStatement statement = _sqlMap.GetMappedStatement(_statementName); ISqlMapSession session = new SqlMapSession(sqlMap); session.OpenConnection(); IList list = statement.ExecuteQueryForList(session, null); //int firstId = HashCodeProvider.GetIdentityHashCode(list); list = statement.ExecuteQueryForList(session, null); int secondId = HashCodeProvider.GetIdentityHashCode(list); _results["id"] = secondId; _results["list"] = list; session.CloseConnection(); } catch (Exception e) { throw e; } }
/// <summary> /// /// </summary> /// <param name="index"></param> /// <param name="localPageSize"></param> /// <returns></returns> private IList GetList(int index, int localPageSize) { bool isSessionLocal = false; ISqlMapSession session = _mappedStatement.SqlMap.LocalSession; if (session == null) { session = new SqlMapSession(_mappedStatement.SqlMap); session.OpenConnection(); isSessionLocal = true; } IList list = null; try { list = _mappedStatement.ExecuteQueryForList(session, _parameterObject, (index) * _pageSize, localPageSize); } catch { throw; } finally { if ( isSessionLocal ) { session.CloseConnection(); } } return list; }
public void Run() { try { IMappedStatement statement = _sqlMap.GetMappedStatement( _statementName ); ISqlMapSession session = new SqlMapSession(sqlMap); session.OpenConnection(); IList list = statement.ExecuteQueryForList(session, null); //int firstId = HashCodeProvider.GetIdentityHashCode(list); list = statement.ExecuteQueryForList(session, null); int secondId = HashCodeProvider.GetIdentityHashCode(list); _results["id"] = secondId ; _results["list"] = list; session.CloseConnection(); } catch (Exception e) { throw e; } }