?? addscore.aspx.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 + -