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;
}
//先把自己输出,再看有没有孩子,如果有孩子则把所有的孩子输出,否则,把自己关闭-------------很典型的递归应用