.net2json

这篇日志的垃圾评论过于多!关闭评论功能!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections;
 
/// 
/// aboutJson 的摘要说明
/// 
public class AboutJson
{
 
    protected string connString;
    protected string sqlString;
    protected SqlConnection Conn;
    protected DataTable table;
    StringBuilder sb;
 
    protected string _tableName;                   //数据库名
    protected string _parentNodeName;              //表中父亲节点字段名
    protected string _treeIdName;                  //表中的ID字段的名称
    protected string _nodeName;                    //表中的节点字段的名称
    protected string _layerName;                   //表中层字段的名称
    protected int _startTid;
 
 
    public AboutJson()
    {
        this.connString = ConfigurationManager.ConnectionStrings[1].ConnectionString;
    }
 
    public AboutJson(string connString)
    {
        this.connString = connString;
    }
 
    public string tableName
    {
        set
        {
            _tableName = value.Trim();
        }
    }
 
    public string parentNodeName
    {
        set
        {
            _parentNodeName = value.Trim();
        }
    }
 
    public string treeIdName
    {
        set
        {
            _treeIdName = value.Trim();
        }
    }
 
    public string nodeName
    {
        set
        {
            _nodeName = value.Trim();
        }
    }
 
    public string layerName
    {
        set
        {
            _layerName = value.Trim();
        }
    }
 
    public DataTable sqlSelectToTable()
    {
        SqlConnection _conn = new SqlConnection(connString);
        _conn.Open();
        SqlCommand _comm = new SqlCommand(sqlString, _conn);
        DataSet _ds = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(_comm);
        adapter.Fill(_ds, "table1");
        table = _ds.Tables["table1"];
        _conn.Close();
        return table;
    }
 
 
    /// 
    /// 传回0层数据
    /// 
    /// 传送回jsonString
    public string buildTreeHeader()
    {
        sb = new StringBuilder("[");
        this.sqlString = "SELECT * FROM ["+_tableName+"] WHERE ["+_parentNodeName+"]=-1";
        DataTable dt = this.sqlSelectToTable();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
           writeRowJson(dt.Rows[i]);
        }
        sb.Remove(sb.Length - 1, 1);
        if (sb.Length >= 1)
        {
            sb.Append("]");
        }
        return sb.ToString();
    }
 
 
    /// 
    /// 查找一个ID后面所有的数据,并以Ext读取的JSON树方式传回
    /// 
    /// 表示起始的ID
    /// 传送回jsonString
    public string buildTree(int startTid)       
    {
        string json;
        sb = new StringBuilder("[");
        _startTid = startTid;
        this.sqlString = "SELECT * FROM [" + _tableName + "] WHERE [" + _parentNodeName + "]!=-1";
        DataTable dt = this.sqlSelectToTable();
        for (int i = 0; i < dt.Rows.Count; i++)//
        {
            if ( int.Parse(dt.Rows[i][_parentNodeName].ToString()) == _startTid)
            {
                wrJson(dt.Rows[i]);
            }
        }
        sb.Remove(sb.Length - 1, 1);
        if (sb.Length >= 1)
        {
            sb.Append("]");
        }
        json = sb.ToString();
        json = json.Replace(",children:[]","");
        json = json.Replace(",]'","]");
        return json;
 
    }
 
 
 
    protected void writeRowJson(DataRow dr)
    {
        string _iNodeId = dr[_treeIdName].ToString();
        string _sNodeName = dr[_nodeName].ToString();
        sb.Append("{id:'" +_treeIdName+"_"+_iNodeId + "',text:'" + _sNodeName);
        sb.Append("'},");
    }
 
    protected void wrJson(DataRow dr)
    {
        string _iNodeId = dr[_treeIdName].ToString();
        string _sNodeName = dr[_nodeName].ToString();
        sb.Append("{id:'" + _treeIdName + "_" + _iNodeId + "',text:'" + _sNodeName);
        writeChild(int.Parse(_iNodeId));
        sb.Append("'},");
    }
 
    protected void writeChild(int parentId)
    {
 
        sb.Append(",children:[");
 
        for (int i = 0; i < table.Rows.Count; i++)
        {
            int iParNode = int.Parse(table.Rows[i][_parentNodeName].ToString());
            if (iParNode == parentId)
            {
                wrJson(table.Rows[i]); 
            }
        }
 
        sb.Append("]");
    }
}

“.net2json”   No comments

No comments yet.

Leave a comment

Comments are closed.