public static void AddTopicPoll(int postid, string question, SortedList<int, string> choices) { IPoll dal = Factory<IPoll>.Create("Poll"); PollInfo poll = new PollInfo {TopicId = postid, DisplayText = question}; foreach (var choice in choices) { PollChoiceInfo pollchoice = new PollChoiceInfo {DisplayText = choice.Value, Order = choice.Key}; poll.AddChoice(pollchoice); } dal.Add(poll); }
public int Add(PollInfo poll) { string pollSql = "INSERT INTO " + Config.ForumTablePrefix + "POLLS (DisplayText,TopicId) VALUES (@Question,@TopicId)"; List<OleDbParameter> parms = new List<OleDbParameter> { new OleDbParameter("@TopicId", OleDbType.Numeric) {Value = poll.TopicId}, new OleDbParameter("@Question", OleDbType.VarChar) {Value = poll.DisplayText} }; int pollid = Convert.ToInt32(SqlHelper.ExecuteInsertQuery(SqlHelper.ConnString, CommandType.Text, pollSql, parms.ToArray())); StringBuilder choices = new StringBuilder(); foreach (var choice in poll.Choices) { choices.AppendFormat("INSERT INTO {0}POLLANSWERS (PollID,DisplayText,SortOrder) VALUES (@PollId,'{1}',{2})",Config.ForumTablePrefix, choice.DisplayText, choice.Order); } if (poll.Choices.Count > 0) SqlHelper.ExecuteNonQuery(SqlHelper.ConnString, CommandType.Text, choices.ToString(), new OleDbParameter("@PollId", OleDbType.Numeric) { Value = pollid }); return pollid; }
public static void UpdateTopicPoll(int pollId, string question, SortedList<int, string> choices) { PollInfo poll = new PollInfo {Id = pollId, DisplayText = question,Choices = null}; foreach (var choice in choices) { if (poll.Choices == null) poll.Choices = new List<PollChoiceInfo>(); poll.Choices.Add(new PollChoiceInfo {DisplayText = choice.Value,Order = choice.Key,PollId = pollId}); } IPoll dal = Factory<IPoll>.Create("Poll"); dal.Update(poll); }
public PollInfo GetById(int id) { string strSql = "SELECT PollId,DisplayText,TopicId FROM " + Config.ForumTablePrefix + "POLLS WHERE PollId=@PollId"; PollInfo poll = null; using (OleDbDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnString, CommandType.Text, strSql, new OleDbParameter("@PollID", OleDbType.Numeric) { Value = id })) { while (rdr.Read()) { poll = new PollInfo { Id = rdr.GetInt32(0), DisplayText = rdr.SafeGetString(1), TopicId = rdr.GetInt32(2) }; } } return poll; }
public void Delete(PollInfo poll) { string pollSql = "DELETE FROM " + Config.ForumTablePrefix + "POLLS WHERE PollId=@PollId"; SqlHelper.ExecuteNonQuery(SqlHelper.ConnString, CommandType.Text, pollSql, new OleDbParameter("@PollId", OleDbType.Numeric) { Value = poll.Id }); }
public void Update(PollInfo poll) { string pollSql = "UPDATE " + Config.ForumTablePrefix + "POLLS SET DisplayText=@Question WHERE PollId=@PollId"; List<OleDbParameter> parms = new List<OleDbParameter> { new OleDbParameter("@PollId", OleDbType.Numeric) {Value = poll.Id}, new OleDbParameter("@Question", OleDbType.VarChar) {Value = poll.DisplayText} }; SqlHelper.ExecuteNonQuery(SqlHelper.ConnString, CommandType.Text, pollSql, parms.ToArray()); if (poll.Choices == null) return; var choices = GetPollChoices(poll.Id).ToArray(); int currentchoicecount = choices.Length; int newchoicecount = poll.Choices.Count; for (int ch = 0; ch < choices.Count(); ch++) { if (ch < newchoicecount) { choices[ch].DisplayText = poll.Choices[ch].DisplayText; choices[ch].Order = poll.Choices[ch].Order; UpdateChoice(choices[ch]); } else { break; } } if (newchoicecount > currentchoicecount) { for (int i = currentchoicecount; i < newchoicecount; i++) { PollChoiceInfo choice = new PollChoiceInfo { DisplayText = poll.Choices[i].DisplayText, PollId = poll.Id, Order = poll.Choices[i].Order }; AddChoice(choice); } } else if (newchoicecount < currentchoicecount) { for (int i = newchoicecount; i < currentchoicecount; i++) { DeleteChoice(choices[i].Id); } } }