这几天偶尔出现评论丢失的现象
这在使用XmlDataProvider时未出现过
具体表现为:提交评论后提示发表成功,但数据库中没有数据。
查看代码:
try
{
Post.AddComment(comment);
//其它代码
}
catch (Exception ex)
{
_Callback = ex.Message;
}
在
Post.AddComment(comment);中
public void AddComment(Comment comment)
{
CancelEventArgs e = new CancelEventArgs();
OnAddingComment(comment, e);
if (!e.Cancel)
{
Comments.Add(comment);
BlogService.AddComment(comment);
OnCommentAdded(comment);
SendNotifications(comment);
}
}
BlogService最终会调用BlogProvider的InsertComment(Comment)方法,这里的BlogProvider是SqlDataProvider,继续跟进:
public override void InsertComment(Comment comment)
{
List<SqlParameter> list = new List<SqlParameter>();
string sql = "INSERT INTO be_PostComment (PostCommentID, PostID, CommentDate, Author, Email, Website, Comment, Country, Ip, IsApproved) " +
"VALUES (@postcommentid, @id, @date, @author, @email, @website, @comment, @country, @ip, @isapproved)";
list.Add(new SqlParameter("@postcommentid", comment.Id.ToString()));
list.Add(new SqlParameter("@id", comment.Parent.Id.ToString()));
list.Add(new SqlParameter("@date", DateTime.Now));
list.Add(new SqlParameter("@author", comment.Author));
list.Add(new SqlParameter("@email", comment.Email));
list.Add(new SqlParameter("@website", comment.Website.ToString() ?? ""));
list.Add(new SqlParameter("@comment", comment.Content));
list.Add(new SqlParameter("@country", comment.Country ?? ""));
list.Add(new SqlParameter("@ip", comment.IP));
list.Add(new SqlParameter("@isapproved", comment.IsApproved));
try
{
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, sql, list.ToArray());
}
catch (Exception ex)
{
SqlLogs.Log(ex.Message, "Exception");
throw ex;
}
Post p = Post.Load(comment.Parent.Id);
p.Comments.Add(comment);
p.Comments.Sort();
}
如果是入库失败的话,日志表里是应该有记录的,而此时日志表是空的,说明很有可能根本就没有走到InsertComment方法 -_-
用了个笨招,直接将InsertComment写到CommentView.ascx.cs,明天再找原因。
在此给热心回复却无故丢失了评论的朋友说声抱歉。