-2

良い一日の仲間!現在、Modified Pre-order Tree Traversal(MPTT)と呼ばれるこのデータベースモデルの設計を研究しています。パフォーマンスが低いためにCommonTableExpressions(CTE)を使用することの欠点を確認した後、MPTTを使用することをお勧めします。ただし、MPTTの利点を利用する前に、「右」と「左」のノード値を追加してデータベーステーブルを再設計する必要があります。そのためには、テーブル内の各データの値を自動化および更新するプログラムを作成する必要があります。私の問題は、ノード値を自動化するプログラムを作成できないことです。PHP言語をC#コードに変換しようとしていますが、変換できませんでした。プログラミングの私の弱点の1つは、「再帰的」メソッドを作成することです。

このリンクを参照として使用しています。 階層型データベースモデル

そして、これが私がC#に変換しようとしているコードです

<?php 
function rebuild_tree($parent, $left) {   
    // the right value of this node is the left value + 1   
    $right = $left+1;   

    // get all children of this node   
    $result = mysql_query('SELECT title FROM tree '.   
                           'WHERE parent="'.$parent.'";');  
    while ($row = mysql_fetch_array($result)) {   
        // recursive execution of this function for each   
        // child of this node   
        // $right is the current right value, which is   
        // incremented by the rebuild_tree function   
        $right = rebuild_tree($row['title'], $right);   
    }   

    // we've got the left value, and now that we've processed
    // the children of this node we also know the right value   
    mysql_query('UPDATE tree SET lft='.$left.', rgt='.   
                 $right.' WHERE title="'.$parent.'";');   

    // return the right value of this node + 1   
    return $right+1;   
}   
?>
4

1 に答える 1

0

これが私自身のコードです。もっと良い解決策があるかどうか教えてください

    int right = 0;

    public int rebuild_tree(int parent, int left)
    {
        // the right value of this node is the left value + 1
        right = left + 1;

        // get all children of this node
        command = conn.CreateCommand();
        command.CommandText = "SELECT cat_id FROM tbl_RefDataCategory_mst WHERE parent_id=@parent";
        command.Parameters.Add("parent", SqlDbType.Int).Value = parent;

        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                int temp = Convert.ToInt32(reader["cat_id"]);
                right = rebuild_tree(temp, right);
            }

            command = conn.CreateCommand();
            command.CommandText = "UPDATE tbl_RefDataCategory_mst SET lft=@l, rgt=@r WHERE cat_id=@p";
            command.Parameters.Add("l", SqlDbType.Int).Value = left;
            command.Parameters.Add("r", SqlDbType.Int).Value = right;
            command.Parameters.Add("p", SqlDbType.Int).Value = parent;

            command.ExecuteNonQuery();
        }

        return right + 1;
    }
于 2012-06-05T01:00:41.803 に答える