/// <summary>
/// 简单类型信息
/// </summary>
public class SimpleTypeInfo
{
private int simpleTypeId;
private string typeName=string.Empty;
private int parentId;
private SimpleTypeInfo parentInfo=null;
private SimpleTypeInfoCollection childs=null;
public int SimpleTypeID
{
get{return simpleTypeId;}
set{simpleTypeId=value;}
}
public string TypeName
{
get{return typeName;}
set{typeName=value;}
}
public int ParentID
{
get{return parentId;}
set{parentId=value;}
}
public SimpleTypeInfo ParentInfo
{
get{return parentInfo;}
set{parentInfo=value;}
}
public SimpleTypeInfoCollection Childs
{
get{return childs;}
set{childs=value;}
}
public SimpleTypeInfo(){}
public SimpleTypeInfo(int simpleTypeId,string typeName,int parentId,SimpleTypeInfo parentInfo,SimpleTypeInfoCollection childs)
{
this.simpleTypeId=simpleTypeId;
this.typeName=typeName;
this.parentId=parentId;
this.parentInfo=parentInfo;
this.childs=childs;
}
}
---------------------------------------------------------------------------
/// <summary>
/// 简单类型信息集合
/// </summary>
public class SimpleTypeInfoCollection:CollectionBase
{
public SimpleTypeInfoCollection(){}
public SimpleTypeInfo this[int index]
{
get{return (SimpleTypeInfo)List[index];}
set{List[index]=value;}
}
public int Add(SimpleTypeInfo value)
{
return List.Add(value);
}
public int IndexOf(SimpleTypeInfo value)
{
return List.IndexOf(value);
}
public void Insert(int index,SimpleTypeInfo value)
{
List.Insert(index,value);
}
public void Remove(SimpleTypeInfo value)
{
List.Remove(value);
}
public bool Contains(SimpleTypeInfo value)
{
return List.Contains(value);
}
}
--------------------------------------------------------------
/// <summary>
/// 简单类型接口
/// </summary>
public interface ISimpleType
{
SimpleTypeInfoCollection getTreeTypes();
}
----------------------------------------------------------------
/// <summary>
/// SqlServer实现类
/// </summary>
public class SqlSimpleType:ISimpleType
{
protected string connectionString=@"database=InputDB;server=surely\mysql;user id=sa;password=123";
public SqlSimpleType(){}
#region ISimpleType 成员
/// <summary>
/// 返回树对象
/// </summary>
/// <returns></returns>
public SimpleTypeInfoCollection getTreeTypes()
{
DataTable dt=this.getSimpleTypes(connectionString);
if(dt==null)
return null;
IDictionary all=new Hashtable();
for(int n=0;n<dt.Rows.Count;n++)
{
SimpleTypeInfo info=this.ConvertType(dt.Rows[n]);
if(info!=null)
all.Add(info.SimpleTypeID,info);
}
SimpleTypeInfoCollection types=this.CreateTreeStructure(all);
return types;
}
/// <summary>
/// 根据Hash创建树结构对象
/// </summary>
/// <param name="all"></param>
/// <returns></returns>
protected SimpleTypeInfoCollection CreateTreeStructure(IDictionary all)
{
if(all==null||all.Count<=0)
return null;
IDictionaryEnumerator de=all.GetEnumerator();
SimpleTypeInfoCollection collection=new SimpleTypeInfoCollection();
while(de.MoveNext())
{
SimpleTypeInfo info=(SimpleTypeInfo)de.Value;
//只添加根
if(info.ParentID<=0)
collection.Add(info);
if(all.Contains(info.ParentID))
{
SimpleTypeInfo parentInfo=((SimpleTypeInfo)all[info.ParentID]);
if(parentInfo.Childs==null)
parentInfo.Childs=new SimpleTypeInfoCollection();
parentInfo.Childs.Add(info);
info.ParentInfo=parentInfo;
}
}
return collection;
}
/// <summary>
/// 返回简单类型表中的所有数据
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
protected DataTable getSimpleTypes(string url)
{
string sql="Select * From SimpleType";
SqlCommand cmd=new SqlCommand(sql,new SqlConnection(url));
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
return dt;
}
/// <summary>
/// 转换简单类型信息
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
protected SimpleTypeInfo ConvertType(DataRow dr)
{
if(dr==null)
return null;
SimpleTypeInfo info=new SimpleTypeInfo();
info.SimpleTypeID=Convert.ToInt32(dr["SimpleTypeID"]);
info.TypeName=Convert.ToString(dr["TypeName"]);
info.ParentID=dr["ParentID"]==System.DBNull.Value?0:Convert.ToInt32(dr["ParentID"]);
return info;
}
#endregion
}
-----------------------------------------调用方法----------------------
ISimpleType s=new SqlSimpleType();
SimpleTypeInfoCollection collection=s.getTreeTypes();
collection则是树结构对象
如果数据库数据量很大的话,不要使用本类,时间关系就不写针对大数据量加载方式了. 实现功能动态生成TreeView树,我没理解他这个树的写法,请大家给指点
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货