ID ParentID Text1 0 AAAA
2 1 BBBB 我现在想从这段开始删、把他下面的子节点也全部删了
3 2 CCCC
4 3 DDDD
5 4 HHHH我写了一个方法只能删除到 CCCC 那一行、在下面的就删不了。 public int DeleteTreeByParentId(int parentId)--通过ParentID删除
{
return mode.ExecuteInsert(connectionString, CommandType.Text, string.Format("delete tree WHERE ParentID = {0}", parentId));
}
public int DeleteTreeBId(int id)--通过ID删除
{
DeleteTreeByParentId(id);--将ID当成ParentID 删除
return mode.ExecuteInsert(connectionString, CommandType.Text, string.Format("delete tree where ID = {0}", id));
}这个只能删除两行在下面的就删不了、小弟还是个初学者、望高手抽空帮忙看看、小弟不胜感激。
2 1 BBBB 我现在想从这段开始删、把他下面的子节点也全部删了
3 2 CCCC
4 3 DDDD
5 4 HHHH我写了一个方法只能删除到 CCCC 那一行、在下面的就删不了。 public int DeleteTreeByParentId(int parentId)--通过ParentID删除
{
return mode.ExecuteInsert(connectionString, CommandType.Text, string.Format("delete tree WHERE ParentID = {0}", parentId));
}
public int DeleteTreeBId(int id)--通过ID删除
{
DeleteTreeByParentId(id);--将ID当成ParentID 删除
return mode.ExecuteInsert(connectionString, CommandType.Text, string.Format("delete tree where ID = {0}", id));
}这个只能删除两行在下面的就删不了、小弟还是个初学者、望高手抽空帮忙看看、小弟不胜感激。
{
DeleteTreeByParentId(id);--将ID当成ParentID 删除
return mode.ExecuteInsert(connectionString, CommandType.Text, string.Format("delete tree where ID = {0} or parentId={0}", id));
}
这样应该可以吧
2 1 BBBB 我现在想从这段开始删、把他下面的子节点也全部删了
3 2 CCCC
4 3 DDDD
5 4 HHHH delte tb where id=2
while @@roucount<>0
delete tb where parentid not in(select id from tb)这就是递归
/// 树形控件添加数据
/// </summary>
/// <param name="tView">树控件</param>
/// <param name="table">数据源</param>
/// <param name="parentColName">父关系列名</param>
/// <param name="colName">关系列名</param>
/// <param name="descriptionColName">节点显示文本列名</param>
public static void TreeView_Load(TreeView tView, DataTable table, string parentColName, string colName, string descriptionColName, string colNamebmbh)
{
tView.Nodes.Clear(); if (tView == null || table == null) return;
DataRow[] rows = table.Select(string.Format("{0} ='0'", parentColName), colName);
foreach (DataRow row in rows)
{
//文本信息Text
TreeNode tNode = new TreeNode(Convert.ToString(row[descriptionColName]));
//健值信息ID
tNode.Tag = Convert.ToString(row[colName]).Trim();
tNode.ToolTipText = "部门编号:" + Convert.ToString(row[colNamebmbh]).Trim();
tView.Nodes.Add(tNode);
}
foreach (TreeNode node in tView.Nodes)
{
TreeNode_Load(node, table, parentColName, colName, descriptionColName, colNamebmbh);
} }
/// <summary>
/// 调用子节点
/// </summary>
/// <param name="tNode"></param>
/// <param name="table"></param>
/// <param name="parentColName"></param>
/// <param name="colName"></param>
/// <param name="descriptionColName"></param>
private static void TreeNode_Load(TreeNode tNode, DataTable table, string parentColName, string colName, string descriptionColName, string colNamebmbh)
{
string nodeId = Convert.ToString(tNode.Tag).Trim();
DataRow[] rows = table.Select(string.Format("{0}={1}", parentColName, nodeId), colName);
if (rows.Length == 0) return; foreach (DataRow row in rows)
{
//文本信息Text
TreeNode node = new TreeNode(Convert.ToString(row[descriptionColName]));
//健值信息ID
node.Tag = Convert.ToString(row[colName]);
tNode.Nodes.Add(node);
node.ToolTipText = "部门编号:" + Convert.ToString(row[colNamebmbh]).Trim();
TreeNode_Load(node, table, parentColName, colName, descriptionColName, colNamebmbh);
}
}比较通用的树形
while @@roucount<>0
delete tb where parentid not in(select id from tb)
支持这段代码
while @@roucount <>0
delete tb where parentid not in(select id from tb)
支持这段代码
insert into @tb select 1,0,'AAAA'
insert into @tb select 2,1,'BBBB'
insert into @tb select 3,2,'CCCC'
insert into @tb select 4,3,'DDDD'
insert into @tb select 5,4,'HHHH'
insert into @tb select 6,1,'eeee'delete @tb where id=2
while @@rowcount<>0
delete @tb where parentid not in(select id from @tb ) and parentid<>0select * from @tbID ParentID Text
1 0 AAAA
6 1 eeee
然后再调用之前写好的DELETE方法进行删除。
protected void DB()
{
DataSet dsp = SqlHelper.ExecuteDataset(DataBase.StrConn, CommandType.Text, "select * from t_test where parentid='0'");
if (dsp.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsp.Tables[0].Rows.Count; i++)
{
TreeNode tnp = new TreeNode();
tnp.Text = dsp.Tables[0].Rows[i]["name"].ToString();
tnp.Value = dsp.Tables[0].Rows[i]["id"].ToString();
TreeView1.Nodes.Add(tnp);
DBchild(Convert.ToInt32(dsp.Tables[0].Rows[i]["id"].ToString()), tnp);
}
}
}
//绑定TREEVIEW
protected void DBchild(int upID, System.Web.UI.WebControls.TreeNode tNode)
{
DataSet ds = SqlHelper.ExecuteDataset(DataBase.StrConn, CommandType.Text, "select * from t_test where parentid='"+upID+"'");
if (ds.Tables[0].Rows.Count > 0)
{
foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["name"].ToString();
tn.Value = dr["id"].ToString();
//tn.NavigateUrl = "";
tn.Target = "right";
tNode.ChildNodes.Add(tn);
DBchild(int.Parse(dr["id"].ToString().Trim()), tn);
}
}
} public string str = ""; //返回子节点ID的方法,返回值为一个字符串
string nodes(string pnode)
{
string strnode = "";
DataSet dsc = SqlHelper.ExecuteDataset(DataBase.StrConn, CommandType.Text, "select id from t_test where parentid='" + pnode + "'");
if (dsc.Tables[0].Rows.Count > 0)
{
for (int k = 0; k < dsc.Tables[0].Rows.Count; k++)
{
strnode += dsc.Tables[0].Rows[k]["id"].ToString() + ",";
str += strnode;
nodes(dsc.Tables[0].Rows[k]["id"].ToString());
}
return str.Remove(str.Length - 1, 1);
}
else
{
return str;
}
}
//选择节点改变时发生的事件
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
string nodesid=nodes(TreeView1.SelectedNode.Value);
if (nodesid.Length > 0)
{
nodesid = TreeView1.SelectedNode.Value + "," + nodesid;
//这里执行删除方法,你自己写一下就可以了。
}
else
{
nodesid = TreeView1.SelectedNode.Value;
//这里执行删除方法,你自己写一下就可以了。
}
Response.Write(nodesid); }