// GET api/<controller> public List <memos> Get() { memos m = new memos(); List <memos> memosList = m.Read(); return(memosList); }
//-------------------------------------------------------------------- // Build the Insert command String //-------------------------------------------------------------------- private String BuildInsertCommand(memos memos) { String command; StringBuilder sb = new StringBuilder(); // use a string builder to create the dynamic string sb.AppendFormat("Values('{0}', '{1}')", memos.TypeId, memos.Text); String prefix = "INSERT INTO memos_g_2021 " + "( [type_id], memo_text) "; command = prefix + sb.ToString(); return(command); }
//--------------------------------------------------------------------------------- // Create the SqlCommand //--------------------------------------------------------------------------------- public List <memos> Readmemos() { SqlConnection con = null; List <memos> memoList = new List <memos>(); try { con = connect("DBConnectionString"); // create a connection to the database using the connection String defined in the web config file String selectSTR = "SELECT * FROM memos_g_2021"; 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 memos b = new memos(); b.Id = Convert.ToInt32(dr["memo_id"]); b.TypeId = Convert.ToInt32(dr["type_id"]); b.Text = (string)dr["memo_text"]; memoList.Add(b); } return(memoList); } catch (Exception ex) { // write to log throw (ex); } finally { if (con != null) { con.Close(); } } }
//=================================matala 4 memos public int Insertmemo(memos memos) { SqlConnection con; SqlCommand cmd; try { con = connect("DBConnectionString"); // create the connection } catch (Exception ex) { // write to log throw (ex); } String cStr = BuildInsertCommand(memos); // 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(); } } }
public void Post([FromBody] memos m) { m.Insert(); }