asp.net(C#)无限栏目分级程序代码分享(3) 核心代码之修改,删除,重置栏目、栏目路径
Posted on 2007-01-31 11:41 Net_Learner 阅读(385) 评论(0) 编辑 收藏 举报
asp.net(C#)无限栏目分级程序代码分享(3) 核心代码之修改,删除,重置栏目、栏目路径
修改,删除,重置栏目、栏目路径
修改栏目#region 修改栏目
public int modiClass(string table,int classid,string classname)
{
string sql;
object temp;
int parentid;
//检测栏目是否存在
sql="select parentid from "+ table + " where classid=" + classid;
temp=base.getvalue(sql);
if (temp==null && temp.ToString()=="")
return 1;//栏目不存在
else
parentid=(int)temp;
//检测同一级栏目里有没有重复名称的
sql="select classid from " + table + " where classname=''" + classname + "'' and parentid=" + parentid + " and classid<>"+ classid;
temp=base.getvalue(sql);
if (temp!=null)
return 2;//同一级栏目已经有重复的
else
{
sql="update "+ table + " set classname=''"+ classname +"'' where classid=" + classid;
base.exesql(sql);
return 0;
}
}
#endregion
栏目map#region 栏目map
public string classMap(string table,int classid)
{
string sql;
object temp;
sql="select parentid from "+ table + " where classid=" + classid;
temp=base.getvalue(sql);
if(temp==null)
return "";
else
{
if ( (int)temp==0)
return "一级栏目";
}
System.Text.StringBuilder sb=new System.Text.StringBuilder();
//栏目路径
string parentpath;
parentpath=(string)base.getvalue("select parentpath from "+ table +" where classid="+ classid );
sql="Select * From "+ table +" where ClassID in (" + parentpath + ") order by Depth";
System.Data.IDataReader dr=base.getdr(sql);
while(dr.Read())
{
int Depth=(int)dr["Depth"];
string classname=(string)dr["classname"];
for(int i=1;i<=Depth;i++){sb.Append(" ");}
if (Depth>0)
sb.Append("└");
sb.AppendFormat(" {0}<br />",classname);
}
dr.Close();
dr.Dispose();
return sb.ToString();
}
#endregion
根据CLASSID得到栏目名称#region 根据CLASSID得到栏目名称
public string getClassName(string table,int classid)
{
string sql;
sql="select classname from "+ table + " where classid=" +classid;
return base.getvalue(sql).ToString();
}
#endregion
复位#region 复位
public int reset(string table)
{
string sql;
System.Collections.ArrayList arr=new System.Collections.ArrayList();
int PrevID,NextID;
PrevID=0;
sql="select ClassID From "+ table +" order by RootID,OrderID";
System.Data.IDataReader dr=base.getdr(sql);
while(dr.Read())
{
arr.Add(dr["classid"]);
}
dr.Close();
dr.Dispose();
for(int j=0;j<arr.Count;j++)
{
if(j==arr.Count-1)
NextID=0;
else
NextID=(int)arr[j+1];
sql="update "+ table +" set RootID=" + arr[j] + ",OrderID="+ (j+1) +",ParentID=0,Child=0,ParentPath=''0'',Depth=0,PrevID=" + PrevID + ",NextID=" + NextID + " where ClassID=" + arr[j];
base.exesql(sql);
PrevID=(int)arr[j];
}
return 0;
}
#endregion
删除栏目#region 删除栏目
public int deleteClass(string table,int classid)
{
string sql;
int Child,PrevID,NextID,Depth,ParentID,orderid;
sql="select ClassID,RootID,Depth,ParentID,Child,PrevID,NextID,orderid From "+ table +" where ClassID="+ classid;
System.Data.IDataReader dr=base.getdr(sql);
if(dr.Read())
{
Child=(int)dr["Child"];
PrevID=(int)dr["PrevID"];
NextID=(int)dr["NextID"];
Depth=(int)dr["Depth"];
ParentID=(int)dr["ParentID"];
orderid=(int)dr["orderid"];
}
else
{
dr.Close();
dr.Dispose();
return 1;//该栏目不存在
}
dr.Close();
dr.Dispose();
if(Child>0)
return 2;//该类别含有子类别
if(Depth>0)
//更新父系栏目的子栏目数目
base.exesql("update "+ table +" set child=child-1 where ClassID=" + ParentID);
//修改上一类别的NextID和下一类别的PrevID
if (PrevID>0)
base.exesql("update "+ table +" set NextID=" + NextID + " where ClassID=" + PrevID);
if (NextID>0)
base.exesql("update "+ table +" set PrevID=" + PrevID + " where ClassID=" + NextID);
//删除栏目
base.exesql("delete from "+ table + " where classid=" + classid);
//这个栏目后面的排序orderid全部减1
base.exesql("update " + table + " set orderid=orderid-1 where orderid>" + orderid);
return 0;//执行成功
}
#endregion
public int modiClass(string table,int classid,string classname)
{
string sql;
object temp;
int parentid;
//检测栏目是否存在
sql="select parentid from "+ table + " where classid=" + classid;
temp=base.getvalue(sql);
if (temp==null && temp.ToString()=="")
return 1;//栏目不存在
else
parentid=(int)temp;
//检测同一级栏目里有没有重复名称的
sql="select classid from " + table + " where classname=''" + classname + "'' and parentid=" + parentid + " and classid<>"+ classid;
temp=base.getvalue(sql);
if (temp!=null)
return 2;//同一级栏目已经有重复的
else
{
sql="update "+ table + " set classname=''"+ classname +"'' where classid=" + classid;
base.exesql(sql);
return 0;
}
}
#endregion
栏目map#region 栏目map
public string classMap(string table,int classid)
{
string sql;
object temp;
sql="select parentid from "+ table + " where classid=" + classid;
temp=base.getvalue(sql);
if(temp==null)
return "";
else
{
if ( (int)temp==0)
return "一级栏目";
}
System.Text.StringBuilder sb=new System.Text.StringBuilder();
//栏目路径
string parentpath;
parentpath=(string)base.getvalue("select parentpath from "+ table +" where classid="+ classid );
sql="Select * From "+ table +" where ClassID in (" + parentpath + ") order by Depth";
System.Data.IDataReader dr=base.getdr(sql);
while(dr.Read())
{
int Depth=(int)dr["Depth"];
string classname=(string)dr["classname"];
for(int i=1;i<=Depth;i++){sb.Append(" ");}
if (Depth>0)
sb.Append("└");
sb.AppendFormat(" {0}<br />",classname);
}
dr.Close();
dr.Dispose();
return sb.ToString();
}
#endregion
根据CLASSID得到栏目名称#region 根据CLASSID得到栏目名称
public string getClassName(string table,int classid)
{
string sql;
sql="select classname from "+ table + " where classid=" +classid;
return base.getvalue(sql).ToString();
}
#endregion
复位#region 复位
public int reset(string table)
{
string sql;
System.Collections.ArrayList arr=new System.Collections.ArrayList();
int PrevID,NextID;
PrevID=0;
sql="select ClassID From "+ table +" order by RootID,OrderID";
System.Data.IDataReader dr=base.getdr(sql);
while(dr.Read())
{
arr.Add(dr["classid"]);
}
dr.Close();
dr.Dispose();
for(int j=0;j<arr.Count;j++)
{
if(j==arr.Count-1)
NextID=0;
else
NextID=(int)arr[j+1];
sql="update "+ table +" set RootID=" + arr[j] + ",OrderID="+ (j+1) +",ParentID=0,Child=0,ParentPath=''0'',Depth=0,PrevID=" + PrevID + ",NextID=" + NextID + " where ClassID=" + arr[j];
base.exesql(sql);
PrevID=(int)arr[j];
}
return 0;
}
#endregion
删除栏目#region 删除栏目
public int deleteClass(string table,int classid)
{
string sql;
int Child,PrevID,NextID,Depth,ParentID,orderid;
sql="select ClassID,RootID,Depth,ParentID,Child,PrevID,NextID,orderid From "+ table +" where ClassID="+ classid;
System.Data.IDataReader dr=base.getdr(sql);
if(dr.Read())
{
Child=(int)dr["Child"];
PrevID=(int)dr["PrevID"];
NextID=(int)dr["NextID"];
Depth=(int)dr["Depth"];
ParentID=(int)dr["ParentID"];
orderid=(int)dr["orderid"];
}
else
{
dr.Close();
dr.Dispose();
return 1;//该栏目不存在
}
dr.Close();
dr.Dispose();
if(Child>0)
return 2;//该类别含有子类别
if(Depth>0)
//更新父系栏目的子栏目数目
base.exesql("update "+ table +" set child=child-1 where ClassID=" + ParentID);
//修改上一类别的NextID和下一类别的PrevID
if (PrevID>0)
base.exesql("update "+ table +" set NextID=" + NextID + " where ClassID=" + PrevID);
if (NextID>0)
base.exesql("update "+ table +" set PrevID=" + PrevID + " where ClassID=" + NextID);
//删除栏目
base.exesql("delete from "+ table + " where classid=" + classid);
//这个栏目后面的排序orderid全部减1
base.exesql("update " + table + " set orderid=orderid-1 where orderid>" + orderid);
return 0;//执行成功
}
#endregion