LigerUi中下拉框一级树tree数据库读取代码示例!

    /// <summary>
    /// 格式化字符型、日期型、布尔型
    /// </summary>

    private static string StringFormat(string str, Type type)
    {
        if (type == typeof(string))
        {
            str = String2Json(str);
            str = "\"" + str + "\"";
        }
        else if (type == typeof(DateTime))
        {
            str = "\"" + str + "\"";
        }
        else if (type == typeof(bool))
        {
            str = str.ToLower();
        }
        else if (type != typeof(string) && string.IsNullOrEmpty(str))
        {
            str = "\"" + str + "\"";
        }
        return str;
    }


    /// <summary>
    /// 过滤特殊字符
    /// </summary>
    private static string String2Json(String s)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.Length; i++)
        {
            char c = s.ToCharArray()[i];
            switch (c)
            {
                case '\"':
                    sb.Append("\\\""); break;
                case '\\':
                    sb.Append("\\\\"); break;
                case '/':
                    sb.Append("\\/"); break;
                case '\b':
                    sb.Append("\\b"); break;
                case '\f':
                    sb.Append("\\f"); break;
                case '\n':
                    sb.Append("\\n"); break;
                case '\r':
                    sb.Append("\\r"); break;
                case '\t':
                    sb.Append("\\t"); break;
                default:
                    sb.Append(c); break;
            }
        }
        return sb.ToString();
    }




        // ----------------------------------------------------------------------- // 取Count数据

        string Sql_Count = "Select Count(Id) as Id from Sys_DepartmentClass" + " Where Deleted<>1 or Deleted is Null";

        Sql_Class SqlClass1 = new Sql_Class();
        SqlDataReader dr1 = SqlClass1.Sql_Select(Sql_Count);
        StringBuilder Count_1 = new StringBuilder();

        while (dr1.Read())
        {
            for (int i = 0; i < dr1.FieldCount; i++)
            {
                Type type = dr1.GetFieldType(i);
                string strKey = dr1.GetName(i);
                string strValue = dr1[i].ToString();
                StrFormat uuu = new StrFormat();
                strValue = uuu.StringFormat(strValue, type);
                
                if (i < dr1.FieldCount - 1)
                { Count_1.Append(strValue); }.
                else
                { Count_1.Append(strValue); }W
            }
        }
        dr1.Close();


        // ----------------------------------------------------------------------- // 取Select数据

        string Sql = "Select DepartmentClass_Id,DepartmentClass_Name from Sys_DepartmentClass"
                  + " Where Deleted<>1 or Deleted is Null";
        Sql_Class SqlClass = new Sql_Class();

        SqlDataReader dr = SqlClass.Sql_Select(Sql);

        StringBuilder jsonString = new StringBuilder();

        jsonString.Append("{");   // 总括号 -- 开始


        jsonString.Append('"' + "Total" + '"' + ":" + '"' + Count_1.ToString() + '"' + ",");   // 字段总数

        jsonString.Append('"' + "Rows" + '"' + ":");   // 行字段开始

        jsonString.Append("[");   // 字段开始
        while (dr.Read())
        {
            jsonString.Append("{");
            for (int i = 0; i < dr.FieldCount; i++)
            {
                Type type = dr.GetFieldType(i);
                string strKey = dr.GetName(i);
                string strValue = dr[i].ToString();
                jsonString.Append("\"" + strKey + "\":");
                strValue = SqlClass.StringFormat(strValue, type);
                if (i < dr.FieldCount - 1)
                {
                    jsonString.Append(strValue + ",");
                }
                else
                {
                    jsonString.Append(strValue);
                }
            }
            jsonString.Append("},");
        }
        dr.Close();
        jsonString.Remove(jsonString.Length - 1, 1);
        jsonString.Append("]");   // 字段结束

        jsonString.Append("}");   // 总括号 -- 结束

        LogManager.WriteLog(jsonString.ToString());

        this.context.Response.Write(jsonString.ToString());        
        
        
    }


[

 {"id":"188","value":null,"text":"中国","icon": null, "fatherid": null, "children":null},

 {"id":"20","value":null,"text":"河北","icon": null, "fatherid": null, "children":null},

 {"id":"21","value":null,"text":"河南","icon": null, "fatherid": null, "children":null},

 {"id":"11","value":null,"text":"河东","icon": null, "fatherid": null, "children":null},

 {"id":"12","value":null,"text":"上海","icon": null, "fatherid": null, "children":null},

 {"id":"166","value":null,"text":"北京","icon": null, "fatherid": null, "children":null},

 {"id":"888","value":null,"text":"天津","icon": null, "fatherid": null, "children": null}

]


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敦厚的曹操

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值