public List <Plant> GetAll() { List <Plant> plants = new List <Plant>(); try { using (SQLiteConnection conn = ConnectionCreater.connect()) { conn.Open(); SQLiteCommand command = new SQLiteCommand("SELECT * FROM plants", conn); using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Plant plant = CreatePlant(reader, command); plants.Add(plant); } } } } catch (SqlException e) { System.Windows.Forms.MessageBox.Show(e.Message); } return(plants); }
private void AutoCmpltTxtField(string sql, TextBox txtField, string column) { txtField.AutoCompleteSource = AutoCompleteSource.CustomSource; txtField.AutoCompleteMode = AutoCompleteMode.SuggestAppend; AutoCompleteStringCollection coll = new AutoCompleteStringCollection(); try { using (SQLiteConnection conn = connectionCreater.connect()) { conn.Open(); SQLiteDataAdapter sda = new SQLiteDataAdapter(sql, conn); DataTable dt = new DataTable(); sda.Fill(dt); for (int i = 0; i < dt.Rows.Count; i++) { string fieldName = dt.Rows[i][column].ToString(); coll.Add(fieldName); } txtField.AutoCompleteCustomSource = coll; } } catch (SqlException e) { MessageBox.Show(e.Message); } }
public void DeleteImageById(int id) { try { using (SqlConnection conn = ConnectionCreater.connect()) { conn.Open(); SqlCommand command = new SqlCommand("Delete FROM pictures WHERE id = @0", conn); command.Parameters.Add(new SqlParameter("0", id)); command.ExecuteNonQuery(); log.Info("Image with the id: " + id + " successfully deleted from the database"); } } catch (SqlException e) { Console.WriteLine(e.Message); log.Error("Sql exception occured while deleting an image from the database", e); } }
public Directory InsertDirectory(Directory directory) { try { using (SqlConnection conn = ConnectionCreater.connect()) { conn.Open(); SqlCommand insertCommand = new SqlCommand("INSERT INTO directories (name, directory_path) VALUES (@name, @directory_path)", conn); insertCommand.Parameters.Add(new SqlParameter("name", directory.Name)); insertCommand.Parameters.Add(new SqlParameter("directory_path", directory.Path)); insertCommand.ExecuteNonQuery(); log.Info("Directory with the name: " + directory.Name + " successfully added to the database"); } } catch (SqlException e) { Console.WriteLine(e.Message); log.Error("Sql exception occured while adding a directory to the database", e); } return(GetDirectoryByName(directory.Name)); }