asp.net(C#)无限栏目分级程序代码(3) 核心代码之添加栏目
代码之添加栏目
添加栏目#region 添加栏目
public object addClass(string table,string classname,int parentid)
{
int rtn=0;
int Child=0;//父系栏目的子栏目数目
int MaxClassID,classid;
int MaxOrderID,rootid;
int previd;
int orderid;
string parentpath="0";
int depth=0;
object c,o,temp_;
c=base.getvalue("select Max(ClassID) From " + table);
o=base.getvalue("select Max(orderid) From " + table);
if(c!=null && c.ToString()!=string.Empty)
MaxClassID=(int)c;
else
MaxClassID=0;
if(o!=null && o.ToString()!="")
MaxOrderID=(int)o;
else
MaxOrderID=0;
o=null;
c=null;
classid=MaxClassID + 1;
rootid=classid;
previd=MaxClassID;
orderid=MaxOrderID+1;
if(parentid>0)
{
//检查是不是有重复的项目
string sql="Select classid From " + table + " Where parentid=" + parentid + " AND ClassName=''" + classname + "''";
//return sql;
if(base.getvalue(sql)!=null)
{
rtn=1;//表示有重复的项目
return rtn;
}
//检查所属类别是否存在
sql="select rootid From " + table + " where classid=" + parentid ;
if(base.getvalue(sql)==null)
{
rtn=2;//表示所属类别不存在
return rtn;
}
else
{
}
sql="select * From " + table + " where ClassID=" + parentid ;
System.Data.IDataReader dr=base.getdr(sql);
if(dr.Read())
{
//重新定义parentpath
parentpath=(string)dr["parentpath"] + "," + parentid;
//重新定义depth
depth=(int)dr["depth"]+1;
//重新定义rootid
rootid=(int)dr["rootid"];
//重新定义Child
Child=(int)dr["Child"];
//重新定义orderid
orderid=(int)dr["orderid"];
}
dr.Close();
dr.Dispose();
//重新定义orderid
//sql="select Max(OrderID) From "+ table +" where rootid=" + rootid;//要注意是刚得到值的rootid
//temp=base.getvalue(sql);
//if(temp!=null && temp.ToString()!="")
//orderid=(int)temp + 1;
//else
//orderid=1;
if(Child>0)
{
//得到属于此父系栏目的所有的栏目的最大的OrderID
sql="select Max(OrderID) From "+ table +" where ParentPath + '','' like ''" + parentpath + ",%''";
orderid=(int)base.getvalue(sql) + 1;
}
else//第一次添加子栏目
{
orderid = orderid + 1;//父系栏目的orderid+1
}
//父栏目的child+1
sql="update "+ table + " set child=child+1 where ClassID=" + parentid ;
base.exesql(sql);
}
//重新定义同一级栏目上一级的nextid
string sql_="update "+ table + " set nextid=" + classid + " where classid=(select top 1 classid from " + table + " where parentid="+ parentid +" order by orderid desc,classid desc)";
base.exesql(sql_);
//重新定义previd
sql_="select classid From " + table + " where ParentID=" + parentid + " order by orderid desc";
temp_=base.getvalue(sql_);
if(temp_!=null)
previd=(int)temp_;
else
previd=0;
//重新修改其他栏目的orderid
sql_="update " + table + " set orderid=orderid+1 where orderid>=" + orderid;
base.exesql(sql_);
//执行插入语句
sql_="insert into "+ table + " (classid,classname,parentid,parentpath,depth,rootid,child,previd,nextid,orderid)";
sql_+="values ("+ classid +",''"+ classname +"'',"+ parentid +",''"+ parentpath +"'',"+ depth +"," + rootid +",0,"+ previd +",0,"+ orderid +")";
base.exesql(sql_);
//base.close();
return 0;
}
#endregion
public object addClass(string table,string classname,int parentid)
{
int rtn=0;
int Child=0;//父系栏目的子栏目数目
int MaxClassID,classid;
int MaxOrderID,rootid;
int previd;
int orderid;
string parentpath="0";
int depth=0;
object c,o,temp_;
c=base.getvalue("select Max(ClassID) From " + table);
o=base.getvalue("select Max(orderid) From " + table);
if(c!=null && c.ToString()!=string.Empty)
MaxClassID=(int)c;
else
MaxClassID=0;
if(o!=null && o.ToString()!="")
MaxOrderID=(int)o;
else
MaxOrderID=0;
o=null;
c=null;
classid=MaxClassID + 1;
rootid=classid;
previd=MaxClassID;
orderid=MaxOrderID+1;
if(parentid>0)
{
//检查是不是有重复的项目
string sql="Select classid From " + table + " Where parentid=" + parentid + " AND ClassName=''" + classname + "''";
//return sql;
if(base.getvalue(sql)!=null)
{
rtn=1;//表示有重复的项目
return rtn;
}
//检查所属类别是否存在
sql="select rootid From " + table + " where classid=" + parentid ;
if(base.getvalue(sql)==null)
{
rtn=2;//表示所属类别不存在
return rtn;
}
else
{
}
sql="select * From " + table + " where ClassID=" + parentid ;
System.Data.IDataReader dr=base.getdr(sql);
if(dr.Read())
{
//重新定义parentpath
parentpath=(string)dr["parentpath"] + "," + parentid;
//重新定义depth
depth=(int)dr["depth"]+1;
//重新定义rootid
rootid=(int)dr["rootid"];
//重新定义Child
Child=(int)dr["Child"];
//重新定义orderid
orderid=(int)dr["orderid"];
}
dr.Close();
dr.Dispose();
//重新定义orderid
//sql="select Max(OrderID) From "+ table +" where rootid=" + rootid;//要注意是刚得到值的rootid
//temp=base.getvalue(sql);
//if(temp!=null && temp.ToString()!="")
//orderid=(int)temp + 1;
//else
//orderid=1;
if(Child>0)
{
//得到属于此父系栏目的所有的栏目的最大的OrderID
sql="select Max(OrderID) From "+ table +" where ParentPath + '','' like ''" + parentpath + ",%''";
orderid=(int)base.getvalue(sql) + 1;
}
else//第一次添加子栏目
{
orderid = orderid + 1;//父系栏目的orderid+1
}
//父栏目的child+1
sql="update "+ table + " set child=child+1 where ClassID=" + parentid ;
base.exesql(sql);
}
//重新定义同一级栏目上一级的nextid
string sql_="update "+ table + " set nextid=" + classid + " where classid=(select top 1 classid from " + table + " where parentid="+ parentid +" order by orderid desc,classid desc)";
base.exesql(sql_);
//重新定义previd
sql_="select classid From " + table + " where ParentID=" + parentid + " order by orderid desc";
temp_=base.getvalue(sql_);
if(temp_!=null)
previd=(int)temp_;
else
previd=0;
//重新修改其他栏目的orderid
sql_="update " + table + " set orderid=orderid+1 where orderid>=" + orderid;
base.exesql(sql_);
//执行插入语句
sql_="insert into "+ table + " (classid,classname,parentid,parentpath,depth,rootid,child,previd,nextid,orderid)";
sql_+="values ("+ classid +",''"+ classname +"'',"+ parentid +",''"+ parentpath +"'',"+ depth +"," + rootid +",0,"+ previd +",0,"+ orderid +")";
base.exesql(sql_);
//base.close();
return 0;
}
#endregion