⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addscore.aspx.cs

📁 一个比较不错的专门为技术型站点订制的专业论坛源码
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace bbs
{
	/// <summary>
	/// Summary description for addscore.
	/// </summary>
	public class addscore : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			string thisURL = "http://" + Request.ServerVariables["SERVER_NAME"].ToString() + Request.ServerVariables["SCRIPT_NAME"].ToString();
			if(IsAuthed())
			{
				if(CheckUser())
				{
					//给帖子加分
					Add();
					//标记帖子为已解决
					MakeUp();
					//更新帖子
					Update();
					//返回
					Response.Redirect("http://" + Request.ServerVariables["SERVER_NAME"].ToString() + "/bbs/topic/" + Request["filename"]);
				}
				else
				{
					Response.Write("对不起,您不是这个帖子的主人或者您企图给自己加分 ~_~");
				}
			}
			else
			{
				Response.Redirect("login.aspx?returnurl=" + Server.UrlEncode(thisURL + "?userid=" + Request["userid"] + "&filename=" + Request["filename"] + "&posttime=" + Request["posttime"]));
			}
		}

		private bool IsAuthed()
		{
			if(Session["userid"] != null)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		private bool CheckUser()
		{
			SqlConnection conn = new SqlConnection((string)Application["ConnectionString"]);
			conn.Open();
			string sql = "select PostUser from BBS where FileName = '" + Request["filename"].Split('.')[0] + "'";
			SqlCommand cmd = new SqlCommand(sql,conn);
			SqlDataReader reader = cmd.ExecuteReader();
			string userid = "";
			try
			{
				reader.Read();
				userid = reader["PostUser"].ToString();
			}
			catch
			{
				Response.Write("数据库读写错误");
			}
			finally
			{
				reader.Close();
				cmd.Dispose();
				conn.Close();
			}

			if(Session["userid"].ToString() == userid && Session["userid"].ToString() != Request["userid"])
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		private void Add()
		{
			SqlConnection conn = new SqlConnection((string)Application["ConnectionString"]);
			conn.Open();
			string sql = "select Score from Board where ID = (select Board from BBS where FileName = '" + Request["filename"].Split('.')[0] + "')";
			SqlCommand cmd = new SqlCommand(sql,conn);
			SqlDataReader reader = cmd.ExecuteReader();
			int Score = 0;
			try
			{
				reader.Read();
				Score = reader.GetInt32(0);
			}
			catch
			{
				Response.Write("数据库读写错误");
			}
			finally
			{
				reader.Close();
				cmd.Dispose();
				conn.Close();
			}

			conn.Open();
			sql = "update Users set Money = Money + " + Score.ToString() + " where ID = " + Request["userid"];
			cmd = new SqlCommand(sql,conn);
			try
			{
				cmd.ExecuteNonQuery();
			}
			catch
			{
				Response.Write("数据库读写错误");
				Response.End();
			}
			finally
			{
				cmd.Dispose();
				conn.Close();
			}
		}

		private void MakeUp()
		{
			SqlConnection conn = new SqlConnection((string)Application["ConnectionString"]);
			conn.Open();
			string sql = "update BBS set Status = '已解决',Solver = '" + Request["userid"] + "' where filename = '" + Request["filename"].Split('.')[0] + "'";
			SqlCommand cmd  = new SqlCommand(sql,conn);
			try
			{
				cmd.ExecuteNonQuery();
			}
			catch
			{
				Response.Write("数据库读写错误");
				Response.End();
			}
			finally
			{
				cmd.Dispose();
				conn.Close();
			}
		}

		private void Update()
		{
			XmlDocument doc = new XmlDocument();
			XmlTextReader reader = new XmlTextReader(Server.MapPath(".") + "\\topic\\" + Request["filename"]);
			doc.Load(reader);
			reader.Close();

			XmlNodeList nodeList;
			XmlElement root = doc.DocumentElement;
			XmlNode status = root.SelectSingleNode("Status");
			status.InnerText = "已解决";
			nodeList = root.SelectNodes("Reply");
			string posttime = "";
			string userid = "";
			foreach (XmlNode node in nodeList)
			{
				XmlNode id = node.SelectSingleNode("Poster/ID");
				userid = id.InnerText;
				XmlNode time = node.SelectSingleNode("PostTime");
				posttime = time.InnerText;
				if(userid == Request["userid"] && posttime == Request["posttime"])
				{
					XmlNode solver = node.SelectSingleNode("Solver");
					solver.InnerText = "YES";
				}
			}

			XmlTextWriter xmlWriter = new XmlTextWriter(Server.MapPath(".") + "\\topic\\" + Request["filename"],null);
			xmlWriter.Formatting = Formatting.Indented;
			try
			{
				doc.Save(xmlWriter);
			}
			catch
			{
				Response.Write("数据库读写出错");
				Response.End();
			}

			xmlWriter.Close();
			reader.Close();
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -