asp.net(C#)无限栏目分级程序代码分享(3) 核心代码之删除栏目树、显示列表
Posted on 2007-01-31 11:38 Net_Learner 阅读(477) 评论(0) 编辑 收藏 举报
asp.net(C#)无限栏目分级程序代码分享(3) 核心代码之删除栏目树、显示列表
核心代码之删除栏目树、显示列表
删除栏目树#region 删除栏目树
public int deleteTree(string table,int classid)
{
string sql;
string parentpath="";
int orderid=0,rootid=0,previd=0,nextid=0,depth=0,parentid=0,child=0;
sql="select * from "+ table + " where classid=" + classid;
IDataReader dr=base.getdr(sql);
if (dr.Read() )
{
parentpath=dr["parentpath"].ToString();
orderid=(int)dr["orderid"];
rootid=(int)dr["rootid"];
previd=(int)dr["previd"];
nextid=(int)dr["nextid"];
depth=(int)dr["depth"];
parentid=(int)dr["parentid"];
child=(int)dr["child"];
}
else
{
dr.Close();
dr.Dispose();
return 1;//栏目不存在
}
dr.Close();
dr.Dispose();
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);
//删除这个栏目
sql="delete from"+ table +" where classid="+ classid ;
base.exesql(sql);
if (child>0)
{
string children=this.getChildren(table,classid);
//得到最大的orderid,以便后面更新orderid
sql="select max(orderid) from " + table + " where classid in ("+ children +")" ;
orderid=(int)base.getvalue(sql);
//删除子栏目
sql="delete from"+ table +" where classid in ("+ children +")" ;
base.exesql(sql);
}
//重新更新orderid
sql="update " + table + " set orderid=orderid-" + (child + 1) + " where orderid>" + orderid;
base.exesql(sql);
return 0;
}
#endregion
显示栏目列表的datatable#region 显示栏目列表的datatable
public DataTable list(string table)
{
string sqlClass;
//sqlClass="select * From " + table + " order by RootID,OrderID";
sqlClass="select * From " + table + " order by OrderID";
return base.getdata(sqlClass);
}
#endregion
得到栏目的排的顺序#region 得到栏目的排的顺序
public int getClassidOrderNum(string table,int classid)
{
string sql;
int i=0;
sql="select classid From " + table + " order by RootID,OrderID";
IDataReader dr=base.getdr(sql);
while(dr.Read())
{
i=i+1;
if( (int)dr["classid"]==classid )
break;
else
{
continue;
}
}
dr.Close();
dr.Dispose();
return i;
}
#endregion
检查栏目是否存在#region 检查栏目是否存在
public bool checkExist(string table,int classid)
{
string sql;
sql="select rootid From " + table + " where classid=" + classid ;
if(base.getvalue(sql)==null)
{
return false;
}
else
{
return true;
}
}
#endregion
罗列一栏目下的所有级别的子栏目#region 罗列一栏目下的所有级别的子栏目
public string getChildren(string table,int classid)
{
System.Text.StringBuilder sb=new System.Text.StringBuilder();
string sql;
if( classid<=0 )
return "";
sql="select classid from "+ table + " where ParentPath + '','' like ''%,"+ classid +",%'' order by orderid";
//_____________________________________________________________________________________按照先后顺序
IDataReader dr=base.getdr(sql);
while(dr.Read())
{
sb.Append(dr[0].ToString() + ",");
}
dr.Close();
dr.Dispose();
string rtn=sb.ToString();
if (rtn!="")
rtn=rtn.Substring(0,rtn.Length-1);//去掉末尾的","
return rtn;
}
#endregion
public int deleteTree(string table,int classid)
{
string sql;
string parentpath="";
int orderid=0,rootid=0,previd=0,nextid=0,depth=0,parentid=0,child=0;
sql="select * from "+ table + " where classid=" + classid;
IDataReader dr=base.getdr(sql);
if (dr.Read() )
{
parentpath=dr["parentpath"].ToString();
orderid=(int)dr["orderid"];
rootid=(int)dr["rootid"];
previd=(int)dr["previd"];
nextid=(int)dr["nextid"];
depth=(int)dr["depth"];
parentid=(int)dr["parentid"];
child=(int)dr["child"];
}
else
{
dr.Close();
dr.Dispose();
return 1;//栏目不存在
}
dr.Close();
dr.Dispose();
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);
//删除这个栏目
sql="delete from"+ table +" where classid="+ classid ;
base.exesql(sql);
if (child>0)
{
string children=this.getChildren(table,classid);
//得到最大的orderid,以便后面更新orderid
sql="select max(orderid) from " + table + " where classid in ("+ children +")" ;
orderid=(int)base.getvalue(sql);
//删除子栏目
sql="delete from"+ table +" where classid in ("+ children +")" ;
base.exesql(sql);
}
//重新更新orderid
sql="update " + table + " set orderid=orderid-" + (child + 1) + " where orderid>" + orderid;
base.exesql(sql);
return 0;
}
#endregion
显示栏目列表的datatable#region 显示栏目列表的datatable
public DataTable list(string table)
{
string sqlClass;
//sqlClass="select * From " + table + " order by RootID,OrderID";
sqlClass="select * From " + table + " order by OrderID";
return base.getdata(sqlClass);
}
#endregion
得到栏目的排的顺序#region 得到栏目的排的顺序
public int getClassidOrderNum(string table,int classid)
{
string sql;
int i=0;
sql="select classid From " + table + " order by RootID,OrderID";
IDataReader dr=base.getdr(sql);
while(dr.Read())
{
i=i+1;
if( (int)dr["classid"]==classid )
break;
else
{
continue;
}
}
dr.Close();
dr.Dispose();
return i;
}
#endregion
检查栏目是否存在#region 检查栏目是否存在
public bool checkExist(string table,int classid)
{
string sql;
sql="select rootid From " + table + " where classid=" + classid ;
if(base.getvalue(sql)==null)
{
return false;
}
else
{
return true;
}
}
#endregion
罗列一栏目下的所有级别的子栏目#region 罗列一栏目下的所有级别的子栏目
public string getChildren(string table,int classid)
{
System.Text.StringBuilder sb=new System.Text.StringBuilder();
string sql;
if( classid<=0 )
return "";
sql="select classid from "+ table + " where ParentPath + '','' like ''%,"+ classid +",%'' order by orderid";
//_____________________________________________________________________________________按照先后顺序
IDataReader dr=base.getdr(sql);
while(dr.Read())
{
sb.Append(dr[0].ToString() + ",");
}
dr.Close();
dr.Dispose();
string rtn=sb.ToString();
if (rtn!="")
rtn=rtn.Substring(0,rtn.Length-1);//去掉末尾的","
return rtn;
}
#endregion