树视图查询

时间:2012-08-02 19:58:00

标签: sql sql-server sql-server-2008 treeview telerik

我创建了一个查询来获取所有地址(孩子)在市政当局(父母),但它回来了空/任何帮助表示赞赏。

drop table #tmp1
go

WITH Emp_CTE AS (
SELECT tablesysID, MunicNo, StreetName       
FROM table1
UNION ALL
SELECT e.tablesysID,e.MunicNo,e.StreetName
FROM table1 e
INNER JOIN Emp_CTE ecte ON ecte.MunicNo = e.MunicNo
)
SELECT *  into  #tmp1
FROM Emp_CTE

select * from #tmp1

它将用于asp.net树视图控件。

感谢Andomar的回答,这是正确的但我只想分享我如何解决这个问题,如下所示:

1:将查询作为存储过程返回xml数据类型作为响应,如:

create PROCEDURE [dbo].[sp_someproc]
    @XmlResponse xml output

AS
BEGIN
    --
    -- Insert statements for procedure here
set @XmlResponse=(SELECT DISTINCT 
               table1.MunicNo + ' ' + table1.StreetName + ' ' + table1.City AS firstRow, table1.MunicNo, table1.StreetName, table1.City, 
                table1.XPOS, table1.YPOS, table1.RollID, table2.Asset_ID, table2.Feature_ID, table2.FeatureName + ',' + table2.Feature_ID + ' ' + table2.[DESC] AS secondRow,
               table2.FeatureName, table2.xxxID, table2.[DESC],table3.WONOs, table3.WONOs + ', ' + table3.AssetType + ', ' +table3.Feature_ID AS workONumber
FROM  table4 INNER JOIN
               table3 ON table4.xxxxID = table3.xxxxID INNER JOIN
               table2 ON table4.Asset_ID = table2.xxx_ID INNER JOIN
               table1 ON table2.StreetName = table1.StreetName AND table3.MunicNo = table1.MunicNo
            for xml auto,root('xml'))
END
select @XmlResponse

2:Aspx页面代码:

<telerik:RadTreeView ID="rtrvxxxxx" runat="server" >
          <DataBindings>
                        <telerik:RadTreeNodeBinding DataMember="table1" TextField="firstRow" ValueField="firstRow" />
                        <telerik:RadTreeNodeBinding DataMember="table2" TextField="secondRow"  />
                         <telerik:RadTreeNodeBinding DataMember="table3" TextField="workONumber"  />

          </DataBindings>               

        </telerik:RadTreeView>

3:C#代码:

private void loadXmlDocument()
        {
            try
            {
                #region Load and Bind xml to treeview

                XmlDataSource xDS = new XmlDataSource();
                XmlDocument xmlDoc = new XmlDocument();

                xmlDoc = callingdatalayerclass.list_XML();
                xDS.Data = xmlDoc.InnerXml;
                xDS.XPath = "/xml/table1";
                xDS.EnableCaching = false;


                //bind to treeview
                rtrvxxxx.DataSource = xDS;
                rtrvxxxx.DataBind();

                #endregion
            }
            catch (Exception ex)
            {
               ex.Message()}
            }
        }

4:数据访问层代码:

public static XmlDocument List_XML()
                    {

                            XmlDocument xmlDoc = new XmlDocument();
                            SqlConnection SQLConn = new SqlConnection();
                            SQLConn.ConnectionString = someclass.someotherclass.GetConnectionString();
                            try
                           {
                            SQLConn.Open();
                            SqlCommand custCMD = new SqlCommand("sp_someproc", SQLConn);

                            custCMD.CommandType = CommandType.StoredProcedure;
                            custCMD.Parameters.Add("@XmlResponse", SqlDbType.Xml).Direction = ParameterDirection.Output;
                            custCMD.ExecuteNonQuery();

                            if (custCMD.Parameters["@XmlResponse"].Value != null)
                            {
                                string xml = custCMD.Parameters["@XmlResponse"].Value.ToString();

                                xmlDoc.LoadXml(xml);
                            }
                            return xmlDoc;

                        }
                        catch (Exception exGEN)
                        {
                            throw exGEN;
                        }
                        finally
                        {
                            SQLConn.Close();

                        }

                    }

注意:如果你看到查询有行是树视图的第一层而第二行是第一行的内行或者可以称之为父节点的子节点,第三行是内行或子行第二行,所以如果第一行有一些记录,它将输出,如果第二行有一些reocrds,它将输出。如果你看到treeview datamember中的数据库部分是表名和 textfiled是firstrow,value field可能是其他一些coulmn。没有使用asp.net原始树视图,所以不知道这是如何工作的这是用于telerik树视图控制。我还发现,如果数据被选为xml,速度会更快。 “DS.XPath =”/ xml / table1“;” &lt; =此代码选择xml元素或根元素,然后在该元素中使用数据排序的第一个元素(第一行记录表)

1 个答案:

答案 0 :(得分:2)

看看你的递归条件:

INNER JOIN Emp_CTE ecte ON ecte.MunicNo = e.MunicNo

这次进入同一个市镇。 (MinicNo is null除外。)

您尚未发布足够的信息以找到正确的条件,但它可能如下所示:

INNER JOIN Emp_CTE ecte ON ecte.tablesysID = e.MunicNo