//-------------------------------------------------------------------- // Build the Insert command String(managerComplaint) //-------------------------------------------------------------------- private String BuildInsertCommand(ManagerComplaint managerComplaint) { String command; StringBuilder sb = new StringBuilder(); // use a string builder to create the dynamic string sb.AppendFormat("Values({0}, {1}, '{2}')", managerComplaint.ManagerID.ToString(), managerComplaint.CompID.ToString(), managerComplaint.SendType); String prefix = "INSERT INTO managerComplaints " + "(managerID, CompID, sendType) "; command = prefix + sb.ToString(); return(command); }
//--------------------------------------------------------------------------------- // Read manager-complaints list //--------------------------------------------------------------------------------- public List <ManagerComplaint> getManComplaintsList() { List <ManagerComplaint> mancomplaintList = new List <ManagerComplaint>(); SqlConnection con = null; // classEX enter your code here try { con = connect("DBConnectionString"); // create a connection to the database using the connection String defined in the web config file String selectSTR = "SELECT * FROM managerComplaints"; SqlCommand cmd = new SqlCommand(selectSTR, con); // get a reader SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); // CommandBehavior.CloseConnection: the connection will be closed after reading has reached the end while (dr.Read()) { // Read till the end of the data into a row ManagerComplaint mc = new ManagerComplaint(); mc.ManagerID = Convert.ToInt32(dr["managerID"]); mc.CompID = Convert.ToInt32(dr["CompID"]); mc.SendType = (string)dr["sendType"]; mancomplaintList.Add(mc); } return(mancomplaintList); } catch (Exception ex) { // write to log throw (ex); } finally { if (con != null) { con.Close(); } } }
//-------------------------------------------------------------------------------------------------- // This method inserts a managerComplaint to the managerComplaint table //-------------------------------------------------------------------------------------------------- public int insert(ManagerComplaint managerComplaint) { SqlConnection con; SqlCommand cmd; try { con = connect("DBConnectionString"); // create the connection } catch (Exception ex) { // write to log throw (ex); } String cStr = BuildInsertCommand(managerComplaint); // 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(); } } }
// DELETE api/<controller>/5 public void Delete(int number) { ManagerComplaint mc = new ManagerComplaint(); mc.deleteMC(number); }
// POST api/<controller> public void Post([FromBody] ManagerComplaint[] arr) { ManagerComplaint mc = new ManagerComplaint(); mc.insert(arr); }