public static string AddRecipeItem(Recipe r) { //insert a single recipe entry in the RecipeMaterials table //since adding a bulk of Recipe items (such as from a complex recipe) would be most easily done by a //compound insert which Access conveniently does not support, this just needs to be called multiple times for //each of the recipes in the Foodstuff's list to add. try { //refactoring to build in only those parameters we actually need. OleDbCommand cmd = new OleDbCommand(); string query = "insert into RecipeMaterials(Makes, MadeOf"; cmd.Parameters.Add("?", OleDbType.VarChar).Value = r.Makes; cmd.Parameters.Add("?", OleDbType.VarChar).Value = r.MadeOf; int intParameterCounter = 2; if (r.FractionAmount() != "") { query += " , Quantity"; cmd.Parameters.Add("?", OleDbType.VarChar).Value = r.FractionAmount(); intParameterCounter++; } if (r.Unit != "") { query += ", Unit"; cmd.Parameters.Add("?", OleDbType.VarChar).Value = r.Unit; intParameterCounter++; } query += ") Values(?"; for (int i = 1; i < intParameterCounter; i++) //counter is one-based { query += ",?"; } query += ")"; cmd.CommandText = query; cmd.Connection = conn; DataConnection.OpenConnection(); cmd.ExecuteNonQuery(); return("Item added."); } catch (Exception ex) { return(ex.ToString()); } finally { DataConnection.CloseConnection(); } }
//overrides to allow comparison between objects. //I don't even pretend to know what half of this stuff actually does //thanks to StackOverflow for a guide though... public override bool Equals(object obj) { //if parameter is null return false if (obj == null) { return(false); } //if parameter can't be cast to a Recipe object return false Recipe p = obj as Recipe; if ((System.Object)p == null) { return(false); } //compare contents of each parameter to see if they line up bool match = true; if (strMakes != p.Makes) { match = false; } if (strMadeOf != p.MadeOf) { match = false; } if (strUnit != p.Unit) { match = false; } if (FractionAmount() != p.FractionAmount()) { match = false; } return(match); }
public static string AddRecipeItem(Recipe r) { //insert a single recipe entry in the RecipeMaterials table //since adding a bulk of Recipe items (such as from a complex recipe) would be most easily done by a //compound insert which Access conveniently does not support, this just needs to be called multiple times for //each of the recipes in the Foodstuff's list to add. try { //refactoring to build in only those parameters we actually need. OleDbCommand cmd = new OleDbCommand(); string query = "insert into RecipeMaterials(Makes, MadeOf"; cmd.Parameters.Add("?", OleDbType.VarChar).Value = r.Makes; cmd.Parameters.Add("?", OleDbType.VarChar).Value = r.MadeOf; int intParameterCounter = 2; if (r.FractionAmount() != "") { query += " , Quantity"; cmd.Parameters.Add("?", OleDbType.VarChar).Value = r.FractionAmount(); intParameterCounter++; } if (r.Unit != "") { query += ", Unit"; cmd.Parameters.Add("?", OleDbType.VarChar).Value = r.Unit; intParameterCounter++; } query += ") Values(?"; for (int i = 1; i < intParameterCounter; i++) //counter is one-based { query += ",?"; } query += ")"; cmd.CommandText = query; cmd.Connection = conn; DataConnection.OpenConnection(); cmd.ExecuteNonQuery(); return "Item added."; } catch (Exception ex) { return ex.ToString(); } finally { DataConnection.CloseConnection(); } }