public List<StoryModel> getStory(string user) { List<StoryModel> result = new List<StoryModel>(); string ConStr = ConfigurationManager.ConnectionStrings["TT2data"].ConnectionString; using (SqlConnection data = new SqlConnection(ConStr)) { data.Open(); string UserName = user; SqlCommand sel = new SqlCommand("select id, title, story, likes from Stories where username = '******' order by id desc", data); using (SqlDataReader reader = sel.ExecuteReader()) { while (reader.Read()) { StoryModel d = new StoryModel(); d.id = reader.GetInt32(0); d.title = reader.GetString(1); d.text = reader.GetString(2); d.likes = reader.GetInt32(3); d.cord = this.getCords(d.id); result.Add(d); } } data.Close(); } return result; }
public ActionResult Add() { StoryModel added = new StoryModel(); added.currentUser = User.Identity.Name; added.title = Request["title"]; added.text = Request["text"]; added.likes = 0; Cords c = new Cords(); string temp = Request["cords"]; string[] i = temp.Split(' '); bool n; try { Convert.ToDecimal(i[1]); Convert.ToDecimal(i[2]); n = true; } catch { n = false; } if (n) { c.x = Convert.ToDecimal(i[1]); c.y = Convert.ToDecimal(i[2]); added.cord.Add(c); } database send = new database(); send.addStory(added); return View("Sandbox", i); }
public void addStory(StoryModel arg) { string ConStr = ConfigurationManager.ConnectionStrings["TT2data"].ConnectionString; using (SqlConnection data = new SqlConnection(ConStr)) { data.Open(); SqlCommand insert = new SqlCommand("insert into Stories (title, story, username, likes) values( " + "'"+arg.title +"',"+ "'" + arg.text + "'," + "'" + arg.currentUser +"', 0 )", data); insert.ExecuteNonQuery(); SqlCommand sel = new SqlCommand("select id from Stories where" +" title = '"+ arg.title +"' and" +" username = '******' and" +" likes = "+arg.likes.ToString() + "",data); string id = ""; using (SqlDataReader reader = sel.ExecuteReader() ) { while (reader.Read()) { int i = reader.GetInt32(0); id = i.ToString(); } } SqlCommand insertCords = new SqlCommand("insert into cord (id, xcord, ycord) values "+ "( "+id+", " +arg.cord.First().x.ToString() + ", " +arg.cord.First().y.ToString() + " )" ,data); insertCords.ExecuteNonQuery(); data.Close(); } }