数据库中有如下表user:
id    parentid  displaytext
1     0         AAA
2     0         bbb
3     1         ccc
4     2         ddd
...  1>请用C#语言实现user的数据,根据id与parentid的值生成一棵树,树节点的Text为displaytext字段值。
  2>写一个存储过程,用游标提取id字段值为1的记录放入一个临时表TempA(或表变量),
    从TempA检索到parentid字段值为一1的记录,并返回此记录displaytext字段的值。

解决方案 »

  1.   

    CREATE PROCEDURE [dbo].P1 ASDECLARE 
    @id nvarchar(5),
    @parentid nvarchar(20), 
    @displaytext nvarchar(50),
    @returnValue nvarchar(50); --逐个提取数据
    DECLARE intersectionData   cursor LOCAL for 
    select id,parentid,displaytext from user where id='1';
    OPEN intersectionData;
    fetch next from intersectionData into @id,@parentid,@displaytext;

    --逐个插入记录
    WHILE @@FETCH_STATUS = 0
    BEGIN
    insert into TempA(id,parentid,displaytext) values(@id,@parentid,@displaytext);
    fetch next from intersectionData into @id,@parentid,@displaytext;
    END;
    CLOSE intersectionData;
    DEALLOCATE intersectionData; select @returnValue = displaytext from TempA where parentid='-1';
    return @returnValueGO
      

  2.   

    你的表名不要取user,user是关键字
      

  3.   


    //打开数据库
    string SqlConn = "data source=f-433;initial catalog=fthAQM;user id=sa;password=tenghe";
    SqlConnection _conn = new SqlConnection(SqlConn);
    _conn.Open();//parent节点变量,自己处理
    int n = 3;
    TreeNode[] treelist = new TreeNode[n];//建立树
    TreeView trvMain = new System.Windows.Forms.TreeView();
    this.Controls.Add(trvMain);
    trvMain.Location = new System.Drawing.Point(0, 27);
    trvMain.Name = "trvMain";
    trvMain.Size = new System.Drawing.Size(136, 663);//循环建立节点
    for (int i = 0; i < n; i++)
    {
        //读取子节点
        string mSql = "select id,parentid,displaytext from TABLE1 where parentid='" + i.ToString() + "'";
        DataTable dt = new DataTable("test");
        SqlCommand mySqlDataSetCmd = new SqlCommand(mSql, _conn);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = mySqlDataSetCmd;
        adapter.Fill(dt);    //建立父节点及子节点
        treelist[i] = new TreeNode(i.ToString());
        TreeNode[] treenodlist = new TreeNode[dt.Rows.Count];    for (int j = 0; j < dt.Rows.Count; j++)
        {
            treenodlist[j] = new TreeNode();
            treenodlist[j].Name = dt.Rows[j][0].ToString();
            treenodlist[j].Text = dt.Rows[j][2].ToString();        treelist[i].Nodes.Add(treenodlist[j]);
        }
        
    }trvMain.Nodes.AddRange(treelist);