public int insert(FieldEq fe) { SqlConnection con; SqlCommand cmd; try { con = connect("database"); } catch (Exception ex) { throw (ex); } String cStr = BuildInsertCommand(fe); // cmd = CreatCommmand(cStr, con); cmd = CreateCommand(cStr, con); try { int numEffected = cmd.ExecuteNonQuery(); return(numEffected); } finally { if (con != null) { con.Close(); } } }
private String BuildInsertCommand(FieldEq fe) { String command; StringBuilder sb = new StringBuilder(); // use a string builder to create the dynamic string sb.AppendFormat("Values('{0}', '{1}','{2}')", fe.Id, fe.Field, fe.Name); String prefix = "INSERT INTO FieldEquipment_2020" + "(EquipmentId,Field,EquipmentName)"; command = prefix + sb.ToString(); return(command); }
public List <FieldEq> readFieldsEq() { List <FieldEq> FieldsEq = new List <FieldEq>(); SqlConnection con = null; try { con = connect("database"); } catch (Exception ex) { throw (ex); } try { string selectSTR = "SELECT * FROM FieldEquipment_2020"; SqlCommand cmd = new SqlCommand(selectSTR, con); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (dr.Read()) { // Read till the end of the data into a row FieldEq fe = new FieldEq(); fe.Id = Convert.ToInt32(dr["EquipmentId"]); fe.Field = (string)dr["Field"]; fe.Name = (string)dr["EquipmentName"]; FieldsEq.Add(fe); } } catch (Exception ex) { // write to log throw (ex); } finally { if (con != null) { con.Close(); } } return(FieldsEq); }
// POST api/<controller> public FieldEq Post([FromBody] FieldEq fieldEq) { fieldEq.insert(); return(fieldEq); }
// GET api/<controller> public List <FieldEq> Get() { FieldEq fe = new FieldEq(); return(fe.getFieldsEq()); }