尝试从sql server添加数据时删除重复的treenode。 数据被添加到树中,但我似乎无法弄清楚如何删除父节点的重复项。
public partial class SYS_Main : System.Web.UI.Page
{
public TreeNode testerr = new TreeNode();
private List<string> _isdupe = new List<string>();
private List<string> _dupe = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
PopulateRootLevel();
TV_Test.CollapseAll();
}
private void PopulateRootLevel()
{
const string connectionString = ("user=;" +
"password=;server =UsersSQL;" +
"Trusted_Connection=yes;" +
"database=System Details; " +
"connection timeout=30");
// connects to sql DB
// sql connection
SqlConnection objConn = new SqlConnection(connectionString);
// sql queries to server
SqlCommand objCommand = new SqlCommand("SELECT Contract, [Server Name],[IP Address] FROM tblServers where Contract !='' AND [SERVER NAME] !='';", objConn);
SqlCommand testquery = new SqlCommand("select s.contract ,COUNT(*)from [System Details].dbo.tblServers s where contract !='' group by Contract having count (*)>1 ;", objConn);
// data adapater holds values of sql queries
SqlDataAdapter da = new SqlDataAdapter(objCommand);
// datatable to hold query data into nodes
DataTable dt = new DataTable();
// data table populated
da.Fill(dt);
//Calls popnodes method and includes dt + nodes for
PopulateNodes2(dt,TV_Test.Nodes);
}
//pop nodes method
private void PopulateNodes2(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TV_Test.Nodes.Clear();
TreeNode IP = new TreeNode();
IP.Text = dr["IP Address"].ToString();
// new instance of a treenode
testerr = new TreeNode();
testerr.Text = dr["Server Name"].ToString();
testerr.ChildNodes.Add(IP);
TreeNode parent = new TreeNode("Text1");
parent.Text = "keyText1";
parent.Text = (dr["Contract"]).ToString();
_isdupe.Add(parent.Text);
parent.ChildNodes.Add(testerr);
nodes.Add(parent);
string trued;
trued = _isdupe.Distinct().ToString();
if (trued.Equals(parent.Text))
{
TV_Test.Nodes.Remove(parent);
}
else
{
}
}
}
}
}
答案 0 :(得分:0)
我认为你的算法错了,如果你订购了你的查询,你就可以直接填写树视图而没有重复。