private String BuildInsertCommand(Tag_labels tag_labels) { String command; StringBuilder sb = new StringBuilder(); // use a string builder to create the dynamic string sb.AppendFormat("Values('{0}', '{1}')", tag_labels.Id_, tag_labels.Name_EN); String prefix = "INSERT INTO Tag_labels_igroup4 " + "(id_,name_EN) "; command = prefix + sb.ToString(); return(command); }
public int insert(Tag_labels tag_labels) { SqlConnection con; SqlCommand cmd; try { con = connect("DBConnectionString"); // create the connection } catch (Exception ex) { // write to log throw (ex); } String cStr = BuildInsertCommand(tag_labels); // 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) { return(0); // write to log throw (ex); } finally { if (con != null) { // close the db connection con.Close(); } } }
// POST api/<controller> public int Post([FromBody] Tag_labels tag_labels) { return(tag_labels.insert(tag_labels)); }