删除一个评论,则回复此评论的所有评论同时删除。可以使用数据库自定义级联删除方式,但数据库要检查数据关联的完备性,为避免麻烦,直接使用函数递归逐步删除数据库内容。达到和级联删除同样的效果
[WebMethod(EnableSession = true)]
public static ResponseResult Delete_Commemt(int commentID)
{
ResponseResult result = new ResponseResult();
string username = (string)HttpContext.Current.Session["username"];
if (username == ""| username == null)
{
result.IsSuccess = false;
result.MsgAlert = "无权这么做";
return result;
}
else
{
string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\storage\\users.accdb";
using (OleDbConnection con = new OleDbConnection(conStr))
{
//查找根据文章id和username查找
string cmdStr = "DELETE FROM comment where [ID]=" + commentID + " and [userName]='" + username + "'";
if (username == "songshizhao" | username == "Admin" | username == "Administrator")//如果是管理员删除评论
{
cmdStr = "DELETE FROM comment where [ID]=" + commentID + "";
}
con.Open();
using (var cmd = new OleDbCommand(cmdStr, con))
{
cmd.ExecuteNonQuery();
}
con.Close();
//找到所有 评论此id 的id
result.IsSuccess = true;
result.MsgAlert = "删除回复成功";
deleteRelitiveComments(commentID);
}
}
return result;
}