C# 查询数据库 并返回json 数据

<%@ WebHandler Language="C#" Class="ObjectstateHandler" %>

using System;
using System.Web;
using System.Reflection;
using System.Data.Odbc;
using System.Text;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Data.DataSetExtensions; 
using System.Data;
using Newtonsoft.Json;
using System.Dynamic;



public class ObjectstateHandler : IHttpHandler, IRequiresSessionState
{


    public class Objectstate
    {

        public string ObjectStateID;

        public string versionID;

        public string state ;

        public string x ;

        public string y ;

        public string stateid ;

        public string ObjectStateDiagramID ;

        public string stateColor ;
        public string Description ;
    }
    
    public class Root
    {
        public int code { get; set; }
        public string msg { get; set; }
        public List <Objectstate> data { get; set; }
        //public string data { get; set; }
    }

    public void ProcessRequest(HttpContext context)
    {
        
        string baseUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path);
        // context.Request.ContentEncoding = Encoding.GetEncoding("gb2312");
        //context.Response.ContentType = "text/plain";

        context.Request.ContentEncoding = Encoding.UTF8;

        using (var connection = new OdbcConnection("Dsn=test;"))  
        {  
            connection.Open();
            string sql = "select * from Objectstate"; 
            using (var command = new OdbcCommand(sql, connection))  
            {  
               // command.CommandType = CommandType.Text;  
                using (var adapter = new OdbcDataAdapter(command))  
                {   
                    var resultTable = new System.Data.DataTable();  
                    adapter.Fill(resultTable);

                    Root root1 = new Root();
                    root1.code = 0;
                    root1.msg = "success";

                    var Objectstates = new List<Objectstate>();


                    //List<Objectstate> data;
                    //List<Objectstate> Objectstate1=new List<Objectstate>();
                    // List<String> bankCodes = resultTable.AsEnumerable().ToList();
                    foreach (DataRow row in resultTable.Rows)
                    {
                        var Objectstate = new Objectstate();
                        Objectstate.ObjectStateID=row["ObjectStateID"].ToString();
                        Objectstate.versionID=row["versionID"].ToString();
                        Objectstate.state=row["state"].ToString();
                        Objectstate.x=row["x"].ToString();
                        Objectstate.y=row["y"].ToString();
                        Objectstate.stateid=row["stateid"].ToString();
                        Objectstate.ObjectStateDiagramID=row["ObjectStateDiagramID"].ToString();
                        Objectstate.stateColor=row["stateColor"].ToString();
                        Objectstate.Description=row["Description"].ToString();
                        Objectstates.Add(Objectstate);
                        // 遍历所有列
                        //foreach (DataColumn column in resultTable.Columns)
                        //{
                        //    context.Response.Write(column.ColumnName +"===="+row[column]);
                        //    context.Response.Write("\r\n");
                            //Objectstate[column.ColumnName]=row[column];
                        //}
                        Objectstates.Add(Objectstate);
                        //context.Response.Write("\r\n\r\n");
                    }

                    root1.data = Objectstates;









                    List<dynamic> ObjectstateList = new List<dynamic>();
                    foreach (DataRow row in resultTable.Rows)
                    {
                        dynamic obj = Activator.CreateInstance(new ExpandoObject().GetType());
                        IDictionary<string, object> dict = obj;
                        foreach (DataColumn column in resultTable.Columns)
                        {
                            dict[column.ColumnName] = row[column.ColumnName];
                        }
                        ObjectstateList.Add(obj);
                    }
                    var RootNew = new
                    {
                        data = ObjectstateList,
                        code = 0,
                        msg = "success"
                    };
                    //context.Response.Write(dataTable.ObjectStateID.value);
                    // var jsonString = JsonConvert.SerializeObject(dataTable);
                    var jsonString = JsonConvert.SerializeObject(RootNew);
                    context.Response.Write(jsonString);

                   // var jsonObj = JsonConvert.DeserializeObject(jsonString);
                   // context.Response.Write(jsonObj);

                }  
            }  
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值