您现在的位置是:首页 > cms教程 > Discuz教程Discuz教程

Discuz!NT2.0没有发现事务还安全吗?

冰露2025-07-02Discuz教程已有人查阅

导读最近在写Blog程序,由于Sql server空间贵,用Access做了但对于C#操作Access还是有些不了解想借鉴一下Discuz!NT没想到找不到事务处理看了Access与Sql版的Discuz!NT2.0

最近在写Blog程序,由于Sql server空间贵,用Access做了但对于C#操作Access还是有些不了解想借鉴一下Discuz!NT没想到找不到事务处理看了Access与Sql版的Discuz!NT2.0竟然没有发现事务的身影?
难道都不需要事务处理了? 如何保证数据的完整性?是不是太想当然了?
这算不算对用户的不负责任?
还是我愚昧找不到保证数据完整性的处理代码? 望高人相助~
附Access与Sql版的删除贴子代码
Access版:
/// <summary>
/// 删除指定ID的帖子
/// </summary>
/// <param name="pid">帖子ID</param>
/// <returns>删除数量</returns>
public static int DeletePost(string posttableid,int pid)
{
// OleDbParameter[] prams = {
// Database.MakeInParam("@pid",OleDbType.Integer,4,pid)
// };
#region 存储过程转sql语句 DeletePost
int fid=0;
int tid=0;
int posterid=0;
int lastforumposterid=0;
int layer=0;
DateTime postdatetime;
string poster="";
int postcount=0;
string title="";
int lasttid=0;
//int postid=0;
int todaycount=0;
string fidlist = "";
string strSQL = "";
DataTable dt=new DataTable();
strSQL = "SELECT [fid], [tid], [posterid],[layer], [postdatetime] FROM [" + BaseConfigFactory.GetTablePrefix + "posts" +posttableid+"] WHERE pid =" + pid;
DataRow dr=Database.ExecuteDataset(CommandType.Text,strSQL).Tables[0].Rows[0];
fid=Convert.ToInt32(dr["fid"].ToString());
tid=Convert.ToInt32(dr["tid"].ToString());
posterid=Convert.ToInt32(dr["posterid"].ToString());
layer=Convert.ToInt32(dr["layer"].ToString());
postdatetime=Convert.ToDateTime(dr["postdatetime"].ToString());
strSQL = "SELECT iif(([parentidlist] is null),'',[parentidlist]) as [fidlist] FROM [" + BaseConfigFactory.GetTablePrefix + "forums] WHERE [fid] =" + fid;
fidlist = Database.ExecuteScalarToStr(CommandType.Text,strSQL);
if (fidlist != "")
{
fidlist = string.Concat(fidlist,",",fid.ToString());
}
else
{
fidlist = fid.ToString();
}
if(layer!=0)
{
//--只删除一个帖子
// --更新论坛总的回帖数
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "statistics] SET [totalpost]=[totalpost] - 1";
Database.ExecuteNonQuery(CommandType.Text,strSQL);
// --更新版块内总的回帖数
if(Convert.ToDateTime(postdatetime).ToShortDateString()==DateTime.Now.ToShortDateString())
{
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "forums] SET [posts]=[posts] - 1, [todayposts]=[todayposts]-1 WHERE [fid] in ("+fidlist+")";
}
else
{
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "forums] SET [posts]=[posts] - 1 WHERE [fid] in ("+fidlist+")";
}
Database.ExecuteNonQuery(CommandType.Text,strSQL);
//--更新用户总的回帖数
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "users] SET [posts] = [posts]-1 WHERE [uid] ="+posterid;
Database.ExecuteNonQuery(CommandType.Text, strSQL);
//--更新主题总的回帖数
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "topics] SET [replies]=[replies] - 1 WHERE [tid]="+tid;
Database.ExecuteNonQuery(CommandType.Text, strSQL);
//--删除帖子
strSQL = "DELETE FROM [" + BaseConfigFactory.GetTablePrefix + "posts" +posttableid+"] WHERE [pid]=" + pid;
Database.ExecuteNonQuery(CommandType.Text, strSQL);
}
else
{
//--删除主题
strSQL = "SELECT COUNT([pid]) FROM [" + BaseConfigFactory.GetTablePrefix + "posts" +posttableid+"] WHERE [tid] = "+tid;
postcount = Convert.ToInt32(Database.ExecuteDataset(CommandType.Text,strSQL).Tables[0].Rows[0][0].ToString());
strSQL = "SELECT COUNT([pid]) FROM [" + BaseConfigFactory.GetTablePrefix + "posts" +posttableid+"] WHERE [tid] ="+tid+" AND DATEDIFF(\"d\", [postdatetime], now()) = 0";
todaycount = Convert.ToInt32(Database.ExecuteDataset(CommandType.Text,strSQL).Tables[0].Rows[0][0].ToString());
//--更新主题及帖子总数
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "statistics] SET [totaltopic]=[totaltopic] - 1, [totalpost]=[totalpost] -"+postcount;
Database.ExecuteNonQuery(CommandType.Text,strSQL);
//--更新版块
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "forums] SET [posts]=[posts] -"+postcount+", [topics]=[topics] - 1,[todayposts]=[todayposts] -"+todaycount+" WHERE [fid] in ("+fidlist+")";
Database.ExecuteNonQuery(CommandType.Text,strSQL);
//--更新用户总的回帖数
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "users] SET [posts] = [posts] - "+postcount+ " WHERE [uid] = "+posterid;
Database.ExecuteNonQuery(CommandType.Text,strSQL);
strSQL = "DELETE FROM [" + BaseConfigFactory.GetTablePrefix + "posts" +posttableid+"] WHERE [tid] = "+tid;
Database.ExecuteNonQuery(CommandType.Text,strSQL);
strSQL = "DELETE FROM [" + BaseConfigFactory.GetTablePrefix + "topics] WHERE [tid] = "+tid;
Database.ExecuteNonQuery(CommandType.Text,strSQL);
}
if (layer != 0)
{
strSQL = "SELECT TOP 1 [pid], [posterid],[postdatetime], [title], [poster] FROM [" + BaseConfigFactory.GetTablePrefix + "posts" +posttableid+"] WHERE [tid]="+tid+" ORDER BY [pid] DESC";
dt=Database.ExecuteDataset(CommandType.Text,strSQL).Tables[0];
if(dt.Rows.Count>0)
{
dr= dt.Rows[0];
pid=Convert.ToInt32(dr["pid"].ToString());
posterid=Convert.ToInt32(dr["posterid"].ToString());
postdatetime=Convert.ToDateTime(dr["postdatetime"].ToString());
title=dr["title"].ToString();
poster=dr["poster"].ToString();
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "topics] SET [lastposter]='"+poster+"',[lastpost]='"+postdatetime.ToString()+"',[lastpostid]="+pid+",[lastposterid]="+posterid+" WHERE [tid]="+tid;
Database.ExecuteNonQuery(CommandType.Text,strSQL);
}
}
strSQL = "SELECT [lasttid] FROM [" + BaseConfigFactory.GetTablePrefix + "forums] WHERE [fid] ="+fid;
lasttid=Convert.ToInt32(Database.ExecuteDataset(CommandType.Text,strSQL).Tables[0].Rows[0][0].ToString());
if(lasttid == tid)
{
strSQL = "SELECT TOP 1 [pid], [tid],[posterid], [title], [poster], [postdatetime] FROM [" + BaseConfigFactory.GetTablePrefix + "posts" +posttableid+"] WHERE [fid] = "+fid+" ORDER BY [pid] DESC";
dt=Database.ExecuteDataset(CommandType.Text,strSQL).Tables[0];
if(dt.Rows.Count>0)
{
dr= dt.Rows[0];
pid=Convert.ToInt32(dr["pid"].ToString());
tid=Convert.ToInt32(dr["tid"].ToString());
if (dr["posterid"] == null)
{
lastforumposterid = 0;
}
else
{
lastforumposterid=Convert.ToInt32(dr["posterid"].ToString());
}
postdatetime=Convert.ToDateTime(dr["postdatetime"].ToString());
if (dr["title"] == null)
{
title = "";
}
else
{
title= dr["title"].ToString();
}
if (dr["poster"] == null)
{
poster = "";
}
else
{
poster=dr["poster"].ToString();
}
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "forums] SET [lasttid]="+tid+",[lasttitle]='"+title+"',[lastpost]='"+postdatetime+"',[lastposter]='"+poster+"',[lastposterid]="+lastforumposterid+" WHERE [fid] in ("+fidlist+")";
Database.ExecuteNonQuery(CommandType.Text,strSQL);
strSQL = "SELECT TOP 1 [pid], [tid], [posterid], [postdatetime], [title], [poster] FROM [" + BaseConfigFactory.GetTablePrefix + "posts" +posttableid+"] WHERE [posterid]="+posterid +" ORDER BY [pid] DESC";
dr= Database.ExecuteDataset(CommandType.Text,strSQL).Tables[0].Rows[0];
pid=Convert.ToInt32(dr["pid"].ToString());
//tid=Convert.ToInt32(dr["tid"].ToString());
posterid=Convert.ToInt32(dr["posterid"].ToString());
postdatetime=Convert.ToDateTime(dr["postdatetime"].ToString());
if (dr["title"] == null)
{
title = "";
}
else
{
title= dr["title"].ToString();
}
//poster=dr["poster"].ToString();
//--更新用户
strSQL = "UPDATE [" + BaseConfigFactory.GetTablePrefix + "users] SET [lastpost] = '"+postdatetime+"',[lastpostid] = "+pid+",[lastposttitle] = '"+title+ "' WHERE [uid] = "+posterid;
Database.ExecuteNonQuery(CommandType.Text,strSQL);
}
}
#endregion
return postcount;
//return Database.ExecuteNonQuery(System.Data.CommandType.StoredProcedure,BaseConfigFactory.GetTablePrefix+"deletepost" + posttableid + "bypid",prams);
}
Sql版存储过程:
CREATE PROCEDURE dnt_deletepost1bypid
@pid int
AS
DECLARE @fid int
DECLARE @tid int
DECLARE @posterid int
DECLARE @lastforumposterid int
DECLARE @layer int
DECLARE @postdatetime smalldatetime
DECLARE @poster varchar(50)
DECLARE @postcount int
DECLARE @title nchar(60)
DECLARE @lasttid int
DECLARE @postid int
DECLARE @todaycount int
SELECT @fid = [fid],@tid = [tid],@posterid = [posterid],@layer = [layer], @postdatetime = [postdatetime] FROM [dnt_posts1] WHERE pid = @pid
DECLARE @fidlist AS VARCHAR(1000)
SET @fidlist = '';
SELECT @fidlist = ISNULL([parentidlist],'') FROM [dnt_forums] WHERE [fid] = @fid
IF RTRIM(@fidlist)<>''
BEGIN
SET @fidlist = RTRIM(@fidlist) + ',' + CAST(@fid AS VARCHAR(10))
END
ELSE
BEGIN
SET @fidlist = CAST(@fid AS VARCHAR(10))
END
IF @layer<>0
BEGIN
UPDATE [dnt_statistics] SET [totalpost]=[totalpost] - 1
UPDATE [dnt_forums] SET
[posts]=[posts] - 1,
[todayposts]=CASE
WHEN DATEPART(yyyy, @postdatetime)=DATEPART(yyyy,GETDATE()) AND DATEPART(mm, @postdatetime)=DATEPART(mm,GETDATE()) AND DATEPART(dd, @postdatetime)=DATEPART(dd,GETDATE()) THEN [todayposts] - 1
ELSE [todayposts]
END
WHERE (CHARINDEX(',' + RTRIM([fid]) + ',', ',' +
(SELECT @fidlist AS [fid]) + ',') > 0)
UPDATE [dnt_users] SET
[posts] = [posts] - 1
WHERE [uid] = @posterid
UPDATE [dnt_topics] SET [replies]=[replies] - 1 WHERE [tid]=@tid
DELETE FROM [dnt_posts1] WHERE [pid]=@pid
END
ELSE
BEGIN
SELECT @postcount = COUNT([pid]) FROM [dnt_posts1] WHERE [tid] = @tid
SELECT @todaycount = COUNT([pid]) FROM [dnt_posts1] WHERE [tid] = @tid AND DATEDIFF(d, [postdatetime], GETDATE()) = 0
UPDATE [dnt_statistics] SET [totaltopic]=[totaltopic] - 1, [totalpost]=[totalpost] - @postcount
UPDATE [dnt_forums] SET [posts]=[posts] - @postcount, [topics]=[topics] - 1,[todayposts]=[todayposts] - @todaycount WHERE (CHARINDEX(',' + RTRIM([fid]) + ',', ',' +(SELECT @fidlist AS [fid]) + ',') > 0)
UPDATE [dnt_users] SET
[posts] = [posts] - @postcount
WHERE [uid] = @posterid
DELETE FROM [dnt_posts1] WHERE [tid] = @tid
DELETE FROM [dnt_topics] WHERE [tid] = @tid
END
IF @layer<>0
BEGIN
SELECT TOP 1 @pid = [pid], @posterid = [posterid], @postdatetime = [postdatetime], @title = [title], @poster = [poster] FROM [dnt_posts1] WHERE [tid]=@tid ORDER BY [pid] DESC
UPDATE [dnt_topics] SET [lastposter]=@poster,[lastpost]=@postdatetime,[lastpostid]=@pid,[lastposterid]=@posterid WHERE [tid]=@tid
END
SELECT @lasttid = [lasttid] FROM [dnt_forums] WHERE [fid] = @fid
IF @lasttid = @tid
BEGIN
SELECT TOP 1 @pid = [pid], @tid = [tid],@lastforumposterid = [posterid], @title = [title], @postdatetime = [postdatetime], @poster = [poster] FROM [dnt_posts1] WHERE [fid] = @fid ORDER BY [pid] DESC
UPDATE [dnt_forums] SET
[lasttid]=@tid,
[lasttitle]=ISNULL(@title,''),
[lastpost]=@postdatetime,
[lastposter]=ISNULL(@poster,''),
[lastposterid]=ISNULL(@lastforumposterid,'0')
WHERE (CHARINDEX(',' + RTRIM([fid]) + ',', ',' +
(SELECT @fidlist AS [fid]) + ',') > 0)
SELECT TOP 1 @pid = [pid], @tid = [tid],@posterid = [posterid], @postdatetime = [postdatetime], @title = [title], @poster = [poster] FROM [dnt_posts1] WHERE [posterid]=@posterid ORDER BY [pid] DESC
UPDATE [dnt_users] SET
[lastpost] = @postdatetime,
[lastpostid] = @pid,
[lastposttitle] = ISNULL(@title,'')
WHERE [uid] = @posterid

本文标签:

很赞哦! ()

相关源码

  • (自适应)蓝色沙盘复古建筑模型制作网站模板源码下载为建筑沙盘模型企业设计的响应式网站模板,通过三维空间展示技术结合产品参数可视化,有效提升模型作品的线上呈现效果与客户咨询转化率。查看源码
  • (自适应)pbootcms模板五金元件气缸气动系统源码下载基于PbootCMS核心开发的气缸气动系统网站模板,为五金元件、气动设备制造企业设计。采用响应式布局技术,自动适配手机、平板等移动设备,确保各类终端用户获得良好浏览体验。查看源码
  • (自适应)个人图集图片相册画册pbootcms网站模板源码本模板基于PbootCMS系统开发,为图片展示类网站设计,特别适合个人作品集、摄影画册、艺术图集等内容展示。采用响应式布局技术,确保各类图片在不同设备上查看源码
  • 响应式pbootcms模板新闻资讯博客自媒体门户网站行业通用解决方案为新闻资讯、自媒体博客打造的响应式网站模板,同时支持企业门户、行业资讯等多场景快速适配。通过简单的图文替换即可实现跨行业转型,显著降低建站成本。查看源码
  • (自适应)品牌创意设计作品工作室pbootcms模板下载该模板适用于品牌策划、艺术设计、广告创意公司官网,亦可通过替换图文快速适配其他行;高端创意设计公司工作室网站源码极简代码架构、艺术化视觉布局、企业级功能扩展性。查看源码
  • pbootcms模板(PC+WAP)火锅加盟餐饮美食类带留言源码基于PbootCMS内核深度开发,为火锅、餐饮品牌打造的营销型解决方案。采用红色主题传递行业活力,实现PC与WAP端适配。查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐