/// <summary> /// Finds a User in the database by a username. Returns a UserDO object /// if only 1 User by that username exists in the database otherwise /// the UserDO will be null. /// </summary> public UserDO GetUserByUsername(string username) { UserDO userDO = null; SqlConnection sqlConnection = null; SqlCommand sqlCommand = null; SqlDataAdapter adapter = null; try { sqlConnection = new SqlConnection(_dataSource); sqlCommand = new SqlCommand("OBTAIN_USER_BY_USERNAME", sqlConnection); sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Parameters.AddWithValue("@Username", username); adapter = new SqlDataAdapter(sqlCommand); DataTable userTable = new DataTable(); sqlConnection.Open(); adapter.Fill(userTable); if (userTable.Rows.Count == 1) { userDO = UserDataTableMapper.UserRowToUserDO(userTable.Rows[0]); } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); throw exception; } finally { if (sqlConnection != null) { sqlConnection.Close(); sqlConnection.Dispose(); } if (sqlCommand != null) { sqlCommand.Dispose(); } if (adapter != null) { adapter.Dispose(); } } return(userDO); }
/// <summary> /// Attempts to retreive all the Users from the database. /// </summary> public List <UserDO> GetAllUsers() { List <UserDO> userDOList = new List <UserDO>(); SqlConnection sqlConnection = null; SqlCommand sqlCommand = null; SqlDataAdapter adapter = null; try { sqlConnection = new SqlConnection(_dataSource); sqlCommand = new SqlCommand("OBTAIN_ALL_USERS", sqlConnection); sqlCommand.CommandType = CommandType.StoredProcedure; adapter = new SqlDataAdapter(sqlCommand); DataTable userTable = new DataTable(); sqlConnection.Open(); adapter.Fill(userTable); userDOList = UserDataTableMapper.UserTableToUserList(userTable); } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); throw exception; } finally { if (sqlConnection != null) { sqlConnection.Close(); sqlConnection.Dispose(); } if (sqlCommand != null) { sqlCommand.Dispose(); } if (adapter != null) { adapter.Dispose(); } } return(userDOList); }