public Lector(LectorId lectId, string fName, string sName, string email) { lectorId = lectId; firstName = fName; secondName = sName; this.email = email; }
public void UpdateLector(LectorId lectId, Lector lector) { if (lector == null) { _logger.Log(string.Format("You sent a null lector:\n {0}", nameof(NullReferenceException))); } string sql = "Update Lectors Set " + "firstName = @firstName, secondName = @secondName, email = @email " + "Where lectorId = @lectorId"; try { using (var sqlConnection = new SqlConnection(_connectionString)) { sqlConnection.Open(); using (SqlCommand command = new SqlCommand(sql, sqlConnection)) { command.Parameters.AddWithValue("@lectorId", lectId.Id); command.Parameters.AddWithValue("@firstName", lector.firstName); command.Parameters.AddWithValue("@secondName", lector.secondName); command.Parameters.AddWithValue("@email", lector.email); command.ExecuteNonQuery(); } } } catch (SqlException sqlException) { _logger.Log(string.Format("You have an error with sql:\n {0}", sqlException)); } catch (ArgumentNullException argException) { _logger.Log(string.Format("You have a null argument:\n {0}", argException)); } }
public void DeleteLector(LectorId lectId) { string sql = "Delete from Lectors " + "Where lectorId = @lectorId "; try { using (var sqlConnection = new SqlConnection(_connectionString)) { sqlConnection.Open(); using (SqlCommand command = new SqlCommand(sql, sqlConnection)) { command.Parameters.AddWithValue("@lectorId", lectId.Id); command.ExecuteNonQuery(); } } } catch (SqlException sqlException) { _logger.Log(string.Format("You have an error with sql:\n {0}", sqlException)); } catch (ArgumentNullException argException) { _logger.Log(string.Format("You have a null argument:\n {0}", argException)); } }
public Lector GetLectorById(LectorId lectId) { var result = new Lector(); string sql = "Select LectorId, FirstName, SecondName, Email from Lectors where lectorId=@lectorId"; try { using (var sqlConnection = new SqlConnection(_connectionString)) { sqlConnection.Open(); using (SqlCommand command = new SqlCommand(sql, sqlConnection)) { command.Parameters.AddWithValue("@lectorId", lectId.Id); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { if (!Int32.TryParse(reader["LectorId"].ToString(), out result.lectorId.Id)) { //TODO Add exception Name and Logging throw new FormatException(); } result.firstName = (string)reader["firstName"]; result.secondName = (string)reader["secondName"]; result.email = (string)reader["email"]; } } } } catch (SqlException sqlException) { _logger.Log(string.Format("You have an error with sql:\n {0}", sqlException)); } catch (ArgumentNullException argException) { _logger.Log(string.Format("You have a null argument:\n {0}", argException)); } return(result); }