C#无限栏目分级程序代码分享 好东西
人气:0想了解C#无限栏目分级程序代码分享 好东西的相关内容吗,在本文为您仔细讲解的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#无限栏目分级程序代码分享,好东西,下面大家一起来学习吧。
数据库表的结构必须有以下字段: 各个字段的说明:
3,本示例核心为idb.cs,db.cs和action.cs,分别说明下作用
idb.cs:数据库操作类的接口,代码如下: using System;
using System.Data;
namespace catalog
{
/// <summary>
/// idb 的摘要说明。
/// </summary>
interface idb
{
//
//void open();构造函数当然不能在接口里声明
System.Data.IDbConnection getcon
{
get;
//set;
}
string constr
{
get;
}
System.Data.IDbCommand command(string sql);
int exesql(string sql);
object getvalue(string sql);
void close();
DataTable getdata(string sql);
System.Data.IDataReader getdr(string sql);
}
}
db.cs实例这个接口: using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
//using System.Web;
namespace catalog
{
/// <summary>
/// db 的摘要说明。
/// </summary>
public class db:idb
{
private IDbConnection con;
private IDbCommand cm;
private string dbtype="access";
public db()
{
//
// TODO: 在此处添加构造函数逻辑
//
dbtype=ConfigurationSettings.AppSettings["dbtype"];
if (dbtype==null)
dbtype="";
if (dbtype.ToLower()=="sqlserver")
{
con=new SqlConnection();
cm= new SqlCommand();
}
else
{
con=new OleDbConnection();
cm= new OleDbCommand();
}
string cnstring=ConfigurationSettings.AppSettings["cnstr"];
con.ConnectionString=cnstring;
open();
cm.Connection=con;
}
public db(string constr)
{
//
// TODO: 在此处添加构造函数逻辑
//
dbtype=ConfigurationSettings.AppSettings["dbtype"];
if (dbtype==null)
dbtype="";
if (dbtype.ToLower()=="sqlserver")
{
con=new SqlConnection();
cm= new SqlCommand();
}
else
{
con=new OleDbConnection();
cm= new OleDbCommand();
}
con.ConnectionString=constr;
open();
cm.Connection=con;
}
private void open()
{
con.Open();
}
public System.Data.IDbConnection getcon
{
get{return con;}
//set{};
}
public int exesql(string sql)
{
cm.CommandText=sql;
return cm.ExecuteNonQuery();
}
public object getvalue(string sql)
{
cm.CommandText=sql;
//return cm.ExecuteScalar();
object o=cm.ExecuteScalar();
return o;
}
public void close()
{
cm.Dispose();
con.Close();
con.Dispose();
con=null;
}
public DataTable getdata(string sql)
{
DataTable dt=new DataTable();
if (dbtype.ToLower()=="sqlserver")
{
SqlDataAdapter adapter = new SqlDataAdapter();
cm.CommandText=sql;
adapter.SelectCommand=(SqlCommand)cm;
adapter.Fill(dt);
}
else
{
OleDbDataAdapter adapter = new OleDbDataAdapter();
cm.CommandText=sql;
adapter.SelectCommand=(OleDbCommand)cm;
adapter.Fill(dt);
}
return dt;
}
public IDataReader getdr(string sql)
{
cm.CommandText=sql;
return cm.ExecuteReader();
}
public string constr
{
get{return ConfigurationSettings.AppSettings["cnstr"];}
}
public System.Data.IDbCommand command(string sql)
{
cm.CommandText=sql;
return cm;
}
}
}
C#无限栏目分级程序代码分享[2] 核心类说明
本程序采用C#为脚本编写,同时支持ACCESS/SQL SERVER数据库。
本程序功能:栏目无限分级,栏目的移动,添加,排序,删除(栏目树),操作方便,部署、使用更为简单,提供统一的接口程序。
本程序才开发完毕,难免有错误或者BUG,欢迎提出,不甚感激。
核心类文件方法调用说明
public void deleteAllCatalog(string table) //清空栏目表
public int downClass(string table,int classid) //栏目向下移动一位
public int upClass(string table,int classid)//栏目向上移动一位
public int moveClass(string table,int classid,int target)//栏目的移动
public int deleteTree(string table,int classid)//删除栏目树
public DataTable list(string table)//用于列出栏目列表
public int getClassidOrderNum(string table,int classid)//得到栏目的排序ID
public bool checkExist(string table,int classid)//检查栏目是否存在
public string getChildren(string table,int classid)//列出一个栏目所有的子栏目
public int modiClass(string table,int classid,string classname)//修改栏目
public string classMap(string table,int classid)//栏目导航,地图
public string getClassName(string table,int classid)//得到栏目名称
public int reset(string table)//重新置位全部类别为一级栏目
public int deleteClass(string table,int classid)//删除栏目
public static void itemcreated(Object Sender, System.Web.UI.WebControls.RepeaterItemEventArgs e,string ctlname,string ctlname2)//列栏目的时候的repeater的事件
public static string getOptions(string table,int type,int selected)//用于select的options
public object addClass(string table,string classname,int parentid)//添加类别
C#无限栏目分级程序代码分享[3] 核心代码放送之清空、排序
using System;
using System.Data;
namespace catalog
{
/// <summary>
/// action 的摘要说明。
/// </summary>
public class action:db
{
private static bool[] ShowLine_={false,false,false,false,false,false,false,false,false,false};
private static System.Collections.ArrayList ShowLine=new System.Collections.ArrayList(ShowLine_);
public action()
{
}
public void deleteAllCatalog(string table)
{
string sql="delete from " + table;
base.exesql(sql);
}
public int downClass(string table,int classid)
{
string sql;
sql="select * from " + table + " where classid=" + classid;
int orderid=0,rootid=0,previd=0,nextid=0,depth=0,parentid=0,child=0;
IDataReader dr=base.getdr(sql);
if (dr.Read() )
{
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 (nextid==0)
return 2;//要下降排序的栏目下面没有栏目,无法被下降
if (nextid==0 && previd==0)
return 3;//要下降排序的栏目的这一级别只有它一个,没办法被提升
return upClass(table,nextid);
}
#region 向上移动栏目
public int upClass(string table,int classid)
{
string sql;
//先得到要提升的classid的栏目信息
sql="select * from " + table + " where classid=" + classid;
int orderid=0,rootid=0,previd=0,nextid=0,depth=0,parentid=0,child=0;
IDataReader dr=base.getdr(sql);
if (dr.Read() )
{
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 (previd==0)
return 2;//要提升排序的栏目上面没有栏目,无法被提升
if (nextid==0 && previd==0)
return 3;//要提升排序的栏目的这一级别只有它一个,没办法被提升
//得到上面一个栏目的信息
sql="select * from " + table + " where classid=" + previd;
int orderid_=0,rootid_=0,previd_=0,nextid_=0,depth_=0,parentid_=0,child_=0;
dr=base.getdr(sql);
if (dr.Read() )
{
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 4;//要提升排序的栏目的上面的栏目不存在,被非法删除
}
dr.Close();
dr.Dispose();
//把原来previd=classid的换为previd=previd
sql="update "+ table + " set previd=" + previd + " where previd=" + classid;
base.exesql(sql);
//把原来nextid=previd换为classid
sql="update "+ table + " set nextid=" + classid + " where nextid=" + previd;
base.exesql(sql);
//处理提升的栏目nextid->previd,previd->previd_,orderid->orderid_
sql="update "+ table + " set nextid=" + previd + ",previd=" + previd_ + " where classid=" + classid;
base.exesql(sql);
int child_num=0,child_num_=0;//两个栏目的子栏目数目
string[] temp;
string children,children_;
children="0";
children_="0";
if (child>0)
{
children=getChildren(table,classid);
temp=children.Split('','');
child_num=temp.Length;
}
if (child_>0)
{
children_=getChildren(table,previd);
temp=children_.Split('','');
child_num_=temp.Length;
}
//处理上一个栏目nextid->nextid,previd->classid
sql="update "+ table + " set nextid=" + nextid + ",previd=" + classid + " where classid=" + previd;
base.exesql(sql);
sql="update " + table + " set orderid=orderid - "+ (child_num_+1) + " where classid in ("+ (classid + "," + children) +")";
base.exesql(sql);
sql="update " + table + " set orderid=orderid + "+ (child_num+1) + " where classid in ("+ (previd + "," + children_) +")";
base.exesql(sql);
return 0;
}
#endregion
查看原文:http://ent.omeweb.com/book/content.aspx?id=1804
C#无限栏目分级程序代码分享[3] 核心代码放送之移动栏目
#region 移动栏目
public int moveClass(string table,int classid,int target)
{
if (!checkExist(table,classid) )
return 1;//要移动的栏目不存在
if(target!=0)
{
if(!checkExist(table,target))
return 2;//目标栏目不存在
}
//要移动的类别的所有的子栏目
string moveclassids;
moveclassids=getChildren(table,classid);
string[] arr=moveclassids.Split('','');
string temp="," + moveclassids;
if (temp.IndexOf("," + target.ToString())!=-1)
return 3;//不能指定该类别的下属类别作为所属类别
string sql;
#region //先处理要移动的栏目的信息
string parentpath="";
int orderid=0,rootid=0,previd=0,nextid=0,depth=0,parentid=0,child=0;
int last_orderid=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"];
}
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);
//得到要移动的栏目的子栏目里最大的orderid
if(child>0)
{
sql="select max(orderid) from " + table + " where classid in (" + moveclassids + ")";
last_orderid=(int)base.getvalue(sql);
//处理要移动的类别树后面的栏目的orderid,全部-(arr.Length+1)
base.exesql("update " + table + " set orderid=orderid-" + (arr.Length+1) + " where orderid>" + last_orderid);
}
else
{
last_orderid=orderid;
//处理要移动的类别树后面的栏目的orderid,全部-1
base.exesql("update " + table + " set orderid=orderid-1 where orderid>" + last_orderid);
}
#endregion
#region//处理目标类别的信息
string target_parentpath="0";
int target_orderid=0,target_rootid=0,target_previd=0,target_nextid=0,target_depth=0,target_parentid=0,target_child=0;
int target_last_child_classid=0;
int target_last_orderid=0;
string target_children="";
#region//是不是作为一级栏目
if(target>0)
{
sql="select * from "+ table + " where classid=" + target;
dr=base.getdr(sql);
if (dr.Read() )
{
target_parentpath=dr["parentpath"].ToString()+"," + target;//新的父系路径
target_orderid=(int)dr["orderid"];
target_rootid=(int)dr["rootid"];
target_previd=(int)dr["previd"];
target_nextid=(int)dr["nextid"];
target_depth=(int)dr["depth"];
target_parentid=(int)dr["parentid"];
target_child=(int)dr["child"];
}
dr.Close();
dr.Dispose();
//更新目标栏目的子栏目数目
base.exesql("update "+ table +" set child=child+1 where ClassID=" + target);
target_children=this.getChildren(table,target);
if (target_child>0)
{
//如果有子栏目找到最后一个子栏目的orderid
sql="select max(orderid) from " + table + " where classid in (" + target_children + ")";
target_last_orderid=(int)base.getvalue(sql);
//找到目标类别的一级子类别最后一个classid,并更新它的nextid=classid
sql="select classid from " + table + " where parentid=" + target + " order by orderid desc";
object temp_=base.getvalue(sql);
target_last_child_classid=(int)temp_;
base.exesql("update " + table + " set nextid=" + classid + " where classid=" + target_last_child_classid);
}
else
{
target_last_orderid=target_orderid;
target_last_child_classid=0;
}
if (child>0)
//处理目标类别树后面的栏目的orderid,全部加上(arr.Length+1)
base.exesql("update " + table + " set orderid=orderid + " + (arr.Length+1) + " where orderid>" + target_last_orderid);
else
base.exesql("update " + table + " set orderid=orderid + 1 where orderid>" + target_last_orderid);
}
else//作为一级类别
{
//重新定义target_depth
target_depth=-1;
//重新定义target_last_orderid
if (child >0)
sql="select max(orderid) from " + table + " where classid not in (" + (classid + "," + moveclassids) + ")";
//_______________________________________________________________这里解决当要移动的栏目有子栏目的时候,orderid出现的小问题.
else
sql="select max(orderid) from " + table + " where classid<>" + classid;//有可能要移动的类别在最后一个,这里把它排除
object _temp;
_temp=base.getvalue(sql);
if(_temp!=null)
target_last_orderid=(int)_temp;
//重新定义target_rootid
target_rootid=classid;
//得到previd
sql="select max(classid) from " + table + " where parentid=0 and classid<>" + classid;
_temp=base.getvalue(sql);
if(_temp!=null)
{
target_previd=(int)_temp;
//修改上一个一级栏目的nextid
base.exesql("update " + table + "set nextid="+ classid + " where classid=" + target_previd);
}
}
#endregion
#endregion
#region 综合处理
if (child>0)//要移动的类别有子栏目
{
int depth_d=0;//depth的减量
if(target==0)
depth_d=depth;
else
depth_d=depth-(target_depth+1);
//更新字栏目的父系路径为 target_parentpath + "," + classid ,orderid
for(int x=0;x<arr.Length;x++)
{
//注意这里要解决depth和parentpath的嵌套问题,即要移动的类别的子类别还有子类别
string path;
sql="select parentpath from " + table + " where classid=" + arr[x];
path=base.getvalue(sql).ToString();
path=path.Replace((parentpath + "," + classid),(target_parentpath + "," + classid));
base.exesql("update " + table +" set depth=depth - "+ depth_d +", parentpath=''"+ path + "'',rootid="+ target_rootid +",orderid="+ (target_last_orderid + x + 2) +" where classid=" + arr[x]);
//__________________________________________________________________________________________________________________________________________________________//其父系栏目已经加1,这里至少加2
}
}
if (target>0)//更新要移动的类别的parentpath和previd,rootid,orderid,parentid,depth,nextid,target_previd
base.exesql("update " + table +" set nextid=0,depth="+ (target_depth+1) +",parentid="+ target +", parentpath=''"+ target_parentpath +"'',previd="+ target_last_child_classid +" ,rootid="+ target_rootid +",orderid="+ (target_last_orderid+1) +" where classid =" + classid);
else//区别在于previd
base.exesql("update " + table +" set previd="+ target_previd +",nextid=0,depth="+ (target_depth+1) +",parentid="+ target +", parentpath=''"+ target_parentpath +"'' ,rootid="+ target_rootid +",orderid="+ (target_last_orderid+1) +" where classid =" + classid);
#endregion
return 0;
}
#endregion
详细请看原文:http://ent.omeweb.com/book/content.aspx?id=1802
演示地址:http://ent.omeweb.com/catalog/index.html
加载全部内容