Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

c# - How to generate MSSQL Hierarchy ID for each element for a tree object

I've a requirement like dragable tree in my project. I've successfully converted mssql resultset to json.

But the end user is again dragging and dropping and totally changed the tree structure. Now I have the below json from the client.

[
  {
    "id": 1,
    "title": "1. dragon-breath",
    "items": [
      {
        "id": 10,
        "title": "1. dragon-breath.1",
        "items": [
          {
            "id": 100,
            "title": "1. dragon-breath.1.2",
            "items": [
              {
                "id": 1000,
                "title": "1. dragon-breath.1.2.3",
                "items": [],
                "pos": 3
              }
            ],
            "pos": 2
          },
          {
            "id": 101,
            "title": "1. dragon-breath.1.2",
            "items": [],
            "pos": 2
          }
        ],
        "pos": 1
      }
    ],
    "pos": 1
  },
  {
    "id": 102,
    "title": "1. dragon-breath.1.2",
    "items": [
      {
        "id": 1020,
        "title": "1. dragon-breath.1.2.1",
        "items": [],
        "pos": 1
      }
    ],
    "pos": 2
  },
  {
    "id": 1021,
    "title": "1. dragon-breath.1.2.1",
    "items": [],
    "pos": 1
  }
]

From the above json "pos" property is for MSSQL Hierarchyid value.

I am trying to regenerate the hierarchy id of each element, when this json posted back from the client.

I am requesting you people to help me. (spend 2 days no luck)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Following the link you posted to the other SO question, and then a link from a comment, I see you are using the MSSQL hierarchyid data type (hint: it might have helped if you been a bit clearer about that in your question ;)

From the online help I see there is a Parse function, which takes a string.

So you need to traverse your JSON tree structure, building strings for each element in the form /1/1/3/ for example, and pass those back to your database along with the associated node id.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...