public void ValidateEmpNr(string input, bool valid) { //Arrange, Act and Assert if (valid) { BasicValidator.ValidateEmployeeNr(input, new ServerLogger()); } else { Assert.Throws <Exception>(() => BasicValidator.ValidateDate(input, errorLogger)); } }
/// <summary> /// The update database. /// </summary> /// <param name="projectName"> /// The project Name. /// </param> /// <param name="timeLog"> /// The time Log. /// </param> /// <param name="day"> /// The day. /// </param> /// <param name="empNr"> /// The emp nr. /// </param> /// <exception cref="Exception"> /// Update Failed. /// </exception> public void UpdateDatabase(string projectName, string timeLog, string day, string empNr) { projectName = projectName.Trim(); timeLog = timeLog.Trim(); BasicValidator.ValidateAsNonSpacedString(projectName, ErrorLogger); BasicValidator.ValidateAsNonSpacedString(empNr, ErrorLogger); BasicValidator.ValidateTimeLog(timeLog, ErrorLogger); BasicValidator.ValidateDate(day, ErrorLogger); var tableName = $"Project_{projectName}_Log"; using (var conn = new SqlConnection()) { var command = new SqlCommand($"select count(*) from {tableName} where WorkDay = @day and EmployeeNr = @empNr ", conn); command.Parameters.AddRange(CreateSqlParameters(timeLog, day, empNr)); conn.ConnectionString = ConnectionString; try { conn.Open(); if ((int)command.ExecuteScalar() > 0) { var updateCommand = new SqlCommand($"update {tableName} set TimeLog = @value where WorkDay = @day and EmployeeNr = @empNr ", conn); updateCommand.Parameters.AddRange(CreateSqlParameters(timeLog, day, empNr)); var rowsUpdated = updateCommand.ExecuteNonQuery(); Console.WriteLine($"rows updated: {rowsUpdated}"); } else { var insertCommand = new SqlCommand($"insert into {tableName} (EmployeeNr, WorkDay, TimeLog, Routed) values (@empNr, @day, @value, 0) ", conn); insertCommand.Parameters.AddRange(CreateSqlParameters(timeLog, day, empNr)); var rowsInserted = insertCommand.ExecuteNonQuery(); Console.WriteLine($"rows updated: {rowsInserted}"); } } catch (Exception e) { ErrorLogger.Error(e.Message, trace); } finally { conn.Close(); conn.Dispose(); } } }