您的位置:首页 > 其它

递归(抓出某节点下面的所有的子孙节点)----简单版

2008-05-16 14:42 281 查看
string s = ""; //全局变量

string objectguid = "ca4a781c-8052-44c8-bcef-e96f7fd2c8db";

private void Form1_Load(object sender, EventArgs e)

{

string strCon = "server=localhost\\SQLExpress;database=E2;Integrated Security=true";

SqlConnection conn = new SqlConnection(strCon);

SqlDataAdapter da = new SqlDataAdapter("select ObjectGuid, ParentGuid, InternalID, ID, CompositeKey, Name

from SystemNamespace ORDER BY CompositeKey ASC", conn);

DataSet ds = new DataSet();

da.Fill(ds, "SystemNamespace");

dataGridView1.DataSource = ds.Tables["SystemNamespace"];

for (int i = 0; i < ds.Tables["SystemNamespace"].Rows.Count; i++)

{

if (ds.Tables["SystemNamespace"].Rows[i]["ObjectGuid"].ToString() == objectguid)

{

s += "<" + ds.Tables["SystemNamespace"].Rows[i]["ID"].ToString() + ">" + " ";

s += changetotree(objectguid);

s += "</" + ds.Tables["SystemNamespace"].Rows[i]["ID"].ToString() + ">" + " ";

}

}

}

string changetotree(string obj)

{

string xmlstr = string.Empty;

string strCon = "server=localhost\\SQLExpress;database=E2;Integrated Security=true";

SqlConnection conn = new SqlConnection(strCon);

SqlDataAdapter da = new SqlDataAdapter("select ObjectGuid, ParentGuid, InternalID, ID, CompositeKey, Name

from SystemNamespace ORDER BY CompositeKey ASC", conn);

DataSet ds = new DataSet();

da.Fill(ds, "SystemNamespace");

for (int i = 0; i < ds.Tables["SystemNamespace"].Rows.Count; i++)

{

if (ds.Tables["SystemNamespace"].Rows[i]["ParentGuid"].ToString() == obj)

{

xmlstr += "<" + ds.Tables["SystemNamespace"].Rows[i]["ID"].ToString() + ">" + " ";

xmlstr += changetotree(ds.Tables["SystemNamespace"].Rows[i]["ObjectGuid"].ToString());

xmlstr += "</" + ds.Tables["SystemNamespace"].Rows[i]["ID"].ToString() + ">" + " ";

}

}

return xmlstr;

}

//先把自己输出,再看有没有孩子,如果有孩子则把所有的孩子输出,否则,把自己关闭-------------很典型的递归应用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: