private String BuildInsertCommand(inginDish inginDish) { String command; StringBuilder sb = new StringBuilder(); // use a string builder to create the dynamic string sb.AppendFormat("Values('{0}', '{1}')", inginDish.DishId, inginDish.IngId); String prefix = "INSERT INTO dish_g_2021 " + "( [dishId], [ingId]) "; command = prefix + sb.ToString(); return(command); }
//-------------------------------------------------ing dish public int InsertIng2Dish(inginDish inginDish) { SqlConnection con; SqlCommand cmd; try { con = connect("DBConnectionString"); // create the connection } catch (Exception ex) { // write to log throw (ex); } String cStr = BuildInsertCommand(inginDish); // helper method to build the insert string cmd = CreateCommand(cStr, con); // create the command try { int numEffected = cmd.ExecuteNonQuery(); // execute the command return(numEffected); } catch (Exception ex) { // write to log throw (ex); } finally { if (con != null) { // close the db connection con.Close(); } } }
// POST api/<controller> public void Post(inginDish indDi) { indDi.Insert(); }