// GetDataPage public DataPage <T> GetDataPage <T>(SqlCommand command, int pageIndex, int pageSize) where T : class { SqlDataReader reader = null; DataPage <T> page = new DataPage <T>(); page.PageIndex = pageIndex; page.PageSize = pageSize; try { if (command.Connection.State != ConnectionState.Open) { command.Connection.Open(); } reader = command.ExecuteReader(); if (reader.HasRows) { IDataMapper mapper = new DataMapperFactory().GetMapper(typeof(T)); while (reader.Read()) { // get the data for this row T dto = null; dto = (T)mapper.GetData(reader); page.Data.Add(dto); // If we haven't set the RecordCount yet then set it if (page.RecordCount == 0) { page.RecordCount = mapper.GetRecordCount(reader); } } reader.Close(); } } catch (Exception e) { throw new Exception("Error populating data", e); } finally { if (reader != null) { reader.Close(); } if (_trans == null) { command.Connection.Close(); } } return(page); }
// GetList public List <T> GetList <T>(SqlCommand command) where T : class { SqlConnection dbConnect = new SqlConnection(_DBConnectionString); command.Connection = dbConnect; List <T> dtoList = new List <T>(); try { if (command.Connection.State != ConnectionState.Open) { command.Connection.Open(); } SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { IDataMapper mapper = new DataMapperFactory().GetMapper(typeof(T)); while (reader.Read()) { T dto = null; dto = (T)mapper.GetData(reader); dtoList.Add(dto); } reader.Close(); reader.Dispose(); } } catch (Exception e) { LogMessage(TraceLevel.Error, "Error populating data: " + e.ToString() + "; " + BuildQueryInfoForLogging(command), System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, System.Threading.Thread.CurrentThread.ManagedThreadId); throw new Exception("Error populating data", e); } finally { try { dbConnect.Close(); command.Connection = null; } catch { } dbConnect.Dispose(); dbConnect = null; } // We return either the populated list if there was data, // or if there was no data we return an empty list. return(dtoList); }
// GetList public List <T> GetList <T>(SqlCommand command) where T : class { List <T> dtoList = new List <T>(); SqlDataReader reader = null; try { if (command.Connection.State != ConnectionState.Open) { command.Connection.Open(); } reader = command.ExecuteReader(); if (reader.HasRows) { IDataMapper mapper = new DataMapperFactory().GetMapper(typeof(T)); while (reader.Read()) { T dto = null; dto = (T)mapper.GetData(reader); dtoList.Add(dto); } reader.Close(); } } catch (Exception e) { throw new Exception("Error populating data", e); } finally { if (reader != null) { reader.Close(); } if (_trans == null) { command.Connection.Close(); } } // We return either the populated list if there was data, // or if there was no data we return an empty list. return(dtoList); }
// GetSingle public T GetSingle <T>(SqlCommand command) where T : class { SqlConnection dbConnect = new SqlConnection(_DBConnectionString); command.Connection = dbConnect; T dto = null; try { if (command.Connection.State != ConnectionState.Open) { command.Connection.Open(); } SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { reader.Read(); IDataMapper mapper = new DataMapperFactory().GetMapper(typeof(T)); dto = (T)mapper.GetData(reader); reader.Close(); reader.Dispose(); } } catch (Exception e) { LogMessage(TraceLevel.Error, "Error populating data: " + e.ToString() + "; " + BuildQueryInfoForLogging(command), System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, System.Threading.Thread.CurrentThread.ManagedThreadId); throw new Exception("Error populating data", e); } finally { try { dbConnect.Close(); command.Connection = null; } catch { } dbConnect.Dispose(); dbConnect = null; } // return the DTO, it's either populated with data or null. return(dto); }
// GetSingle public T GetSingle <T>(SqlCommand command) where T : class { T dto = null; SqlDataReader reader = null; try { if (command.Connection.State != ConnectionState.Open) { command.Connection.Open(); } reader = command.ExecuteReader(); if (reader.HasRows) { reader.Read(); IDataMapper mapper = new DataMapperFactory().GetMapper(typeof(T)); dto = (T)mapper.GetData(reader); reader.Close(); } } catch (Exception e) { throw new Exception("Error populating data", e); } finally { if (reader != null) { reader.Close(); } if (_trans == null) { command.Connection.Close(); } } // return the DTO, it's either populated with data or null. return(dto); }
// GetSingle public T GetSingle <T>(string SQLCommandText, Dictionary <string, object> sqlParams, bool throwErrors) where T : class { SQLiteConnection dbBConnection = null; SQLiteCommand query = null; T dto = null; try { // Protect this section with our ReaderWriterLockSlim that handles concurrency issues for us try { // Use a read lock for this operation _SQLiteDBReadWriteLocker.EnterReadLock(); // Open database connection if we're not inside a transaction session if (_Session_sqlTransaction == null) { dbBConnection = new SQLiteConnection(_DBConnectStr); dbBConnection.Open(); } // Build SQL command query = new SQLiteCommand(); if (_Session_sqlTransaction == null) { query.Connection = dbBConnection; } else { query.Connection = _Session_sqlConnection; } query.CommandText = SQLCommandText; query.CommandType = CommandType.Text; // Build and populate SQL command parameters SetQueryParameters(query, sqlParams); SQLiteDataReader reader = query.ExecuteReader(); if (reader.HasRows) { reader.Read(); IDataMapper mapper = new DataMapperFactory().GetMapper(typeof(T)); dto = (T)mapper.GetData(reader); reader.Close(); } } finally { // Release the read lock if (_SQLiteDBReadWriteLocker.IsReadLockHeld) { _SQLiteDBReadWriteLocker.ExitReadLock(); } } } catch (Exception ex) { LogMessage(TraceLevel.Error, ex.ToString() + "; " + BuildQueryInfoForLogging(query), System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, System.Threading.Thread.CurrentThread.ManagedThreadId); if (throwErrors) { throw ex; } } finally { if (query != null) { query.Dispose(); } // If not inside a transaction session, free local db connection if (_Session_sqlTransaction == null) { if (dbBConnection != null) { dbBConnection.Close(); dbBConnection.Dispose(); } } } // return the DTO, it's either populated with data or null. return(dto); }