<%@ 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;
}
}
}
C# 查询数据库 并返回json 数据
最新推荐文章于 2024-08-23 10:35:13 发布