<WebMethod(Description:="JSON Hierarchy Object")> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function xmlData() As Object
Using _ds As DataSet = db.ExecuteDataSet(CommandType.Text, "Select NodeID, NodeName,ParentID from Nodes WHERE ParentID=1")
_ds.DataSetName = "Menus"
_ds.Tables(0).TableName = "Menu"
Dim relation As New DataRelation("ParentChild", _ds.Tables("Menu").Columns("NodeID"), _ds.Tables("Menu").Columns("ParentID"), True)
relation.Nested = True
_ds.Relations.Add(relation)
Dim StrDc As String() = New String(_ds.Tables(0).Columns.Count - 1) {}
Dim HeadStr As String = String.Empty
For i As Integer = 0 To _ds.Tables(0).Columns.Count - 1
StrDc(i) = _ds.Tables(0).Columns(i).Caption
HeadStr += """" + StrDc(i) + """ : """ + StrDc(i) + i.ToString() + "¾" + ""","
Next
HeadStr = HeadStr.Substring(0, HeadStr.Length - 1)
Dim Sb As New StringBuilder()
Sb.Append("{""" + _ds.Tables(0).TableName + """ : [")
For i As Integer = 0 To _ds.Tables(0).Rows.Count - 1
Dim TempStr As String = HeadStr
Sb.Append("{")
For j As Integer = 0 To _ds.Tables(0).Columns.Count - 1
TempStr = TempStr.Replace(_ds.Tables(0).Columns(j).ToString + j.ToString() + "¾", _ds.Tables(0).Rows(i)(j).ToString())
Next
Sb.Append(TempStr + "},")
Next
Sb = New StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1))
Sb.Append("]}")
Return Sb.ToString
End Using
End Function
上記の関数を使用してを返しますが、このSQLクエリでJSON String
使用すると、次のエラーが発生しますParentID=1
Select NodeID, NodeName,ParentID from Nodes WHERE ParentID=1
System.ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.
at System.Data.ConstraintCollection.AddForeignKeyConstraint(ForeignKeyConstraint constraint)
at System.Data.ConstraintCollection.Add(Constraint constraint, Boolean addUniqueWhenAddingForeign)
at System.Data.DataRelationCollection.DataSetRelationCollection.AddCore(DataRelation relation)
at System.Data.DataRelationCollection.Add(DataRelation relation)
at Haggler.xmlData() in D:\2008VS\Haggler\EntLib5\App_Code\Haggler.vb:line 114
テーブル構造:
SqlQueryからWhere句を削除すると正常に動作します。