private void display_All_food(string query) { //Connect to SQL and display the titles of food and sweet from database //Create a list with the queries i need (1 query for food and 1 for sweets) List <string> queryList = new List <string>(); //Create a list and insert all data from database so we can use them everywhere. SQLiteCommand cmd = new SQLiteCommand(query, databaseObject.myConnection); databaseObject.OpenConnection(); SQLiteDataReader dataReader; dataReader = cmd.ExecuteReader(); foodListView.Items.Clear(); //Display the list of foods and sweets depending the query while (dataReader.Read()) { lv_food = new ListViewItem(dataReader[0].ToString()); //Title lv_food.SubItems.Add(dataReader[1].ToString()); //Category lv_food.SubItems.Add(dataReader[2].ToString()); //Ingredients lv_food.SubItems.Add(dataReader[3].ToString()); //Description databaseData_food.Add(dataReader[0].ToString()); // Add title from database into a list. foodListView.Items.Add(lv_food); //Add into listview all the titles from database. ( Because we added 1 collumn) dv = new DataView(dt); } dataReader.Close(); cmd.Dispose(); databaseObject.CloseConnection(); }
private void addItemButton_Click(object sender, EventArgs e) { string query = "Insert into recipies (Title, Category, Ingredients, Description) Values('" + titleTextBox.Text + "','" + dropdownCategory.Text + "','" + addIngredientTextBox.Text + "','" + addDescriptionTextBox.Text + "')"; SQLiteCommand sqlcmd = new SQLiteCommand(query, databaseObject.myConnection); databaseObject.OpenConnection(); var resultExcecution = sqlcmd.ExecuteNonQuery(); databaseObject.CloseConnection(); Console.WriteLine("Rows Added: {0}", resultExcecution); this.Close(); //Closes the form programmatically. }
public static void exportDB() { Database_Items_Class databaseObject = new Database_Items_Class(); //sqlite object Directory.CreateDirectory(folder_Creation); SQLiteDataReader reader; string query = "SELECT * FROM recipies WHERE Title='" + Listof_items_screen.foodTitle + "'"; string strDelimiter = "\n"; string strFilePath = @"C:\Recipies\" + Listof_items_screen.foodTitle + "_recipe.txt";; databaseObject.OpenConnection(); using (reader = new SQLiteCommand(query, databaseObject.myConnection).ExecuteReader()) { if (reader.HasRows) { StringBuilder sb = new StringBuilder(); Object[] items = new Object[reader.FieldCount]; List <Database_Items_Class> itemsFromDb = new List <Database_Items_Class>(); Database_Items_Class item = new Database_Items_Class(); //Create the object and fill it's variables with database data. while (reader.Read()) { reader.GetValues(items); item.Title = reader.GetString(0); item.Category = reader.GetString(1); item.Ingredients = reader.GetString(2); //ingredientsTextBox.Text = reader.GetString(2); item.Description = reader.GetString(3); itemsFromDb.Add(item); //Console.WriteLine(item.Ingredients); // Using two sb.Append is better because if you // concat the two strings it will first build a new string // and then discard it after its use. //sb.Append(strDelimiter); //sb.Append(item.ToString()); } //Write into txt file sb.Append("Title: " + item.Title + "\n"); sb.Append("===========================================================================\n"); sb.Append("Ingredients: \n\n" + item.Ingredients + "\n"); sb.Append("===========================================================================\n"); sb.Append("Description: \n\n" + item.Description + "\n"); File.WriteAllText(strFilePath, sb.ToString()); } } databaseObject.CloseConnection(); }