public void AddResult() { SqlConnection con = mainController.dbConnection.connection; try { SaveToModel(); //Save all fields in result Model if (currentModel.ResultImage != null && !CheckForEmptyField()) { string storedFileName = FtpController.SaveImage(currentModel.ResultImage); //File path for image that is saved with method string query = @"INSERT INTO Resultaat (Gebruiker, Verdunning, Kolonies, Temperatuur, Soort, Strain, Tijdsduur, Voedingsbodem, Bron, Medium, Behandeling, Path, Kolonie_positie) VALUES(" + "@Gebruiker,@Verdunning,@Kolonies,@Temperatuur,@Soort,@Strain,@Tijdsduur,@Voedingsbodem,@Bron,@Medium,@Behandeling,@Path,@Kolonie_positie)"; SqlCommand cmd = new SqlCommand(query, con); cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@Gebruiker", currentModel.Gebruiker); cmd.Parameters.AddWithValue("@Verdunning", currentModel.Verdunning); cmd.Parameters.AddWithValue("@Kolonies", currentModel.ColonyList.Count); cmd.Parameters.AddWithValue("@Temperatuur", currentModel.Temperatuur); cmd.Parameters.AddWithValue("@Soort", currentModel.Soort); cmd.Parameters.AddWithValue("@Strain", currentModel.Strain); cmd.Parameters.AddWithValue("@Tijdsduur", currentModel.Tijdsduur); cmd.Parameters.AddWithValue("@Voedingsbodem", currentModel.Voedingsbodem); cmd.Parameters.AddWithValue("@Bron", currentModel.Bron); cmd.Parameters.AddWithValue("@Medium", currentModel.Medium); cmd.Parameters.AddWithValue("@Behandeling", currentModel.Behandeling); cmd.Parameters.AddWithValue("@Path", storedFileName); cmd.Parameters.AddWithValue("@Kolonie_positie", currentModel.ColonyListToString()); inleesView.currentResult.FilePath = storedFileName; //Open Database connection and execute the SQL query con.Open(); cmd.ExecuteNonQuery(); //Show that the result has been added MessageBox.Show("Resultaat toegevoegd."); } else { MessageBox.Show("Vul a.u.b. alle velden in", "Niet alle velden zijn ingevuld.", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (SqlException sql) { MessageBox.Show(sql.Message, "Database error"); } catch (Exception) { MessageBox.Show("Laad a.u.b. een afbeelding in"); } finally { if (con.State == ConnectionState.Open) { con.Close(); } } }
/// <summary> /// Saves a resultModel to the database /// </summary> /// <param name="resultModel">The resultModel to save.</param> public void ResultModelToDB(ResultModel resultModel) { // CHECK FOR OPEN CONNECTION int connOpen = 0; if (connection.State == ConnectionState.Closed) { connection.Open(); connOpen = 1; } // STRING WITH QUERY string query = @"UPDATE Resultaat SET " + "Verdunning = @verdunning, " + "Kolonies = @kolonies, " + "Temperatuur = @temperatuur, " + "Soort = @soort, " + "Strain = @strain, " + "Tijdsduur = @tijdsduur, " + "Voedingsbodem = @voedingsbodem, " + "Bron = @bron, " + "Medium = @medium, " + "Behandeling = @behandeling, " + "Path = @path, " + "Kolonie_positie = @kolonie_positie " + "WHERE ID = @id"; SqlCommand command = new SqlCommand(query, connection); // QUERY PARAMETERS command.Parameters.Add(new SqlParameter("verdunning", resultModel.Verdunning)); command.Parameters.Add(new SqlParameter("kolonies", resultModel.Kolonies)); command.Parameters.Add(new SqlParameter("temperatuur", resultModel.Temperatuur)); command.Parameters.Add(new SqlParameter("soort", resultModel.Soort)); command.Parameters.Add(new SqlParameter("strain", resultModel.Strain)); command.Parameters.Add(new SqlParameter("tijdsduur", resultModel.Tijdsduur)); command.Parameters.Add(new SqlParameter("voedingsbodem", resultModel.Voedingsbodem)); command.Parameters.Add(new SqlParameter("bron", resultModel.Bron)); command.Parameters.Add(new SqlParameter("medium", resultModel.Medium)); command.Parameters.Add(new SqlParameter("behandeling", resultModel.Behandeling)); command.Parameters.Add(new SqlParameter("path", resultModel.FilePath)); command.Parameters.Add(new SqlParameter("kolonie_positie", resultModel.ColonyListToString())); command.Parameters.Add(new SqlParameter("id", resultModel.ID)); command.ExecuteNonQuery(); // CLEAR PARAMETERS command.Parameters.Clear(); mainController.resultController.Init(); // IF OPEN THEN CLOSE if (connOpen == 1) { connection.Close(); } }
/// <summary> /// Saves a resultModel to the database /// </summary> /// <param name="resultModel">The resultModel to save.</param> public void ResultModelToDB(ResultModel resultModel) { // CHECK FOR OPEN CONNECTION int connOpen = 0; if (connection.State == ConnectionState.Closed) { connection.Open(); connOpen = 1; } // STRING WITH QUERY string query = @"UPDATE Resultaat SET " + "Verdunning = @verdunning, "+ "Kolonies = @kolonies, "+ "Temperatuur = @temperatuur, "+ "Soort = @soort, " + "Strain = @strain, " + "Tijdsduur = @tijdsduur, " + "Voedingsbodem = @voedingsbodem, " + "Bron = @bron, " + "Medium = @medium, " + "Behandeling = @behandeling, " + "Path = @path, " + "Kolonie_positie = @kolonie_positie " + "WHERE ID = @id"; SqlCommand command = new SqlCommand(query, connection); // QUERY PARAMETERS command.Parameters.Add(new SqlParameter("verdunning", resultModel.Verdunning)); command.Parameters.Add(new SqlParameter("kolonies", resultModel.Kolonies)); command.Parameters.Add(new SqlParameter("temperatuur", resultModel.Temperatuur)); command.Parameters.Add(new SqlParameter("soort", resultModel.Soort)); command.Parameters.Add(new SqlParameter("strain", resultModel.Strain)); command.Parameters.Add(new SqlParameter("tijdsduur", resultModel.Tijdsduur)); command.Parameters.Add(new SqlParameter("voedingsbodem", resultModel.Voedingsbodem)); command.Parameters.Add(new SqlParameter("bron", resultModel.Bron)); command.Parameters.Add(new SqlParameter("medium", resultModel.Medium)); command.Parameters.Add(new SqlParameter("behandeling", resultModel.Behandeling)); command.Parameters.Add(new SqlParameter("path", resultModel.FilePath)); command.Parameters.Add(new SqlParameter("kolonie_positie", resultModel.ColonyListToString())); command.Parameters.Add(new SqlParameter("id", resultModel.ID)); command.ExecuteNonQuery(); // CLEAR PARAMETERS command.Parameters.Clear(); mainController.resultController.Init(); // IF OPEN THEN CLOSE if (connOpen == 1) connection.Close(); }