Code
Public void CreateData ()
{
/* Data Source
Common. RunParameter. Instance (). DataLink = @ "Data Source = SP002 \ MSSQLSERVERS; Initial Catalog = SP2008_ZC_01; uid = sa; pwd = ***; Pooling = True ;";
Common. RunParameter. Instance (). DataType = "SqlServer ";
DataTable dt = Database. DBOperate. ExecuteDataset ("Select tGuid, tCode, tName, tRoot, tRank From ScTeam where tDele = 0 Order By tCode"). Tables [0];
// Common. runParameter. instance (). dataLink = @ "Data Source = SP002 \ MSSQLSERVERS; Initial Catalog = SP2008_JX_01; uid = sa; pwd = ***; Pooling = True ;";
// Common. RunParameter. Instance (). DataType = "SqlServer ";
// DataTable dt = Database. DBOperate. ExecuteDataset ("Select rGuid, rCode, rName, rRoot, rRank From ScRace where rDele = 0 Order By rCode"). Tables [0];
*/
JavaScriptSerializer serializer = new JavaScriptSerializer ();
String result = serializer. Serialize (getJson (dt ));
Response. Write (result );
}
Private static Nodes [] getJson (DataTable dt)
{
Var node = new List <Nodes> ();
Foreach (DataRow row in dt. Rows)
{
If (Int. parse (row ["trank"]. tostring ())! = 1) continue;
VaR code = row [1]. tostring ();
VaR r = DT. Select (string. Format ("{0} = '{1}'", DT. Columns [3]. columnname, code ),"");
Nodes N;
If (R. Length = 0)
{
N = new Nodes {id = row [0]. ToString (), text = row [2]. ToString (), leaf = true };
}
Else
{
N = new Nodes {id = row [0]. ToString (), text = row [2]. ToString ()};
N = getNodes (code, dt, n );
}
Node. Add (n );
}
Return node. ToArray ();
}
Private static Nodes getNodes (string code, DataTable dt, Nodes n)
{
Var nc = n;
Foreach (var row in dt. select (string. format ("{0} = '{1}'", dt. columns [3]. columnName, code ),""))
{
Var c = row [1]. ToString ();
Var r = dt. Select (string. Format ("{0} = '{1}'", dt. Columns [3]. ColumnName, c ),"");
Nodes ns;
If (R. Length = 0)
{
NS = new nodes {id = row [0]. tostring (), text = row [2]. tostring (), leaf = true };
}
Else
{
Ns = new Nodes {id = row [0]. ToString (), text = row [2]. ToString ()};
GetNodes (c, dt, ns );
}
Nc. children. Add (ns );
}
Return nc;
}
}
Class Nodes
{
Public string id;
Public string text;
Public bool leaf;
Public List <Nodes> children = new List <Nodes> ();
}
// You only need to point the tree's dataurl to this page.