private String BuildInsertCommand(tinderUser u) { String command; StringBuilder sb = new StringBuilder(); // use a string builder to create the dynamic string sb.AppendFormat("Values({0},'{1}','{2}')", u.UserId, u.UserName, u.UserImage); String prefix = "INSERT INTO likeTable_ori_2020 " + "(UserId,UserName,UserImages)"; command = prefix + sb.ToString(); return(command); }
public List <tinderUser> getAllUsers() { List <tinderUser> allUsers = new List <tinderUser>(); SqlConnection con = null; try { con = connect("tinderUserDBConnectionString"); string selectSTR = "SELECT * from tinderUers_2020_ori "; SqlCommand cmd = new SqlCommand(selectSTR, con); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (dr.Read()) { tinderUser u = new tinderUser(); u.UserId = Convert.ToInt32(dr["UserId"]); u.UserName = (string)dr["UserName"]; u.UserGender = (string)dr["UserGender"]; u.UserAge = Convert.ToInt16(dr["UserAge"]); u.UserHeight = float.Parse(dr["UserHeight"].ToString()); u.UserCity = (string)dr["UserCity"]; u.UserImage = (string)dr["UserImage"]; if (Convert.ToInt16(dr["UserPremium"]) == 1) { u.UserPremium = true; u.UserHobbies = (string)dr["UserHobbies"]; } else { u.UserPremium = false; u.UserHobbies = ""; } allUsers.Add(u); } return(allUsers); } catch (Exception ex) { throw ex; } finally { if (con != null) { con.Close(); } } }
public int inserUser(tinderUser u) { SqlConnection con; SqlCommand cmd; try { con = connect("tinderUserDBConnectionString"); // create the connection } catch (Exception ex) { throw (ex); // write to log } String cStr = BuildInsertCommand(u); // 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] tinderUser u) { return(u.inserUser(u)); }
// GET api/<controller> public List <tinderUser> Get() { tinderUser u = new tinderUser(); return(u.getUsers()); }