我把树形的子节点查询出来放到一个LIST里面,然后想通过SQL查询出数据库里面“名字”字段值 包含在LIST里面的数据,该怎么写呢?
List<string> subNodes = new List<string>();
select * from a where name in subNodes?

解决方案 »

  1.   


    List<string> subNodes = new List<string>();
    string s="";
    foreach(string s1 in subNodes )
    {
      s+= s1 +","
    }
    if(s!="")
    {
     s= s.subString(0,s.Length-1);
    }string sql ="select * from a where name in subNodes in ('"+ s+"')";
    //代码手打,仅提供辅导员参考
      

  2.   

    List<string> subNodes = new List<string>();
    string s=subNodes.Aggregate((p1,p2)=>string.Format("{0},{1}",p1,p2));
    string sql ="select * from a where name in subNodes in ('"+ s+"')";
      

  3.   

     谢谢,已经完成,把代码公布下       
            List<string> subNodes = new List<string>();
            public void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
            {
                a k = new a();
                subNodes.Add(this.treeView2.SelectedNode.Text);
                getnode(e.Node);
                string s="";
    foreach(string s1 in subNodes )
    {
        s += "'" + s1 + "',";
    }
    if(s!="")
    {
     s= s.Substring(0,s.Length-1);
    }
                try
                {
                    dataGridView1.DataSource = k.rds("select * from a where name in (" + s + ") ).Tables[0];                
                }
                catch (Exception)
                { }
                subNodes.Clear();
            }
    public void  getnode(TreeNode sn)
            {
                if (sn.Nodes.Count > 0)
                {
                    foreach (TreeNode subNode in sn.Nodes)
                    {
                        subNodes.Add(subNode.Text);
                        if (sn.Nodes != null)
                        {
                            getnode(subNode);
                        }
                    }
                }
            }