数据库:
use master
go
if exists(select * from sysdatabases where name=’stu1′)
drop database stu1
go
create database stu1
on
(
name=’stu1′,
filename=’D:sqlstu1.mdf’,
size=4,
filegrowth=10%
)
log on
(
name=’stu1_log’,
filename=’D:sqlstu1_log.ldf’,
size=4,
filegrowth=10%
)
go
use stu1
go
if exists(select * from sysobjects where name=’classes’)
drop database classes
go
create table classes
(
id int identity primary key,
name varchar(20),
direction varchar(20)
)
insert into classes
select ‘2012271’,’.NET’ union
select ‘2012272’,’J2EE’ union
select ‘2012273’,’.NET’ union
select ‘2012274’,’J2EE’ union
select ‘2012275’,’J2EE’ union
select ‘2012276’,’.NET’ union
select ‘2012277’ ,’.NET’
go
select * from classes
if exists(select * from sysobjects where name=’student’)
drop table student
go
create table student
(
id int identity primary key,
name varchar(20),
sex bit check(sex in(1,0)),
age int,
address varchar(30),
hobby varchar(30),
c_id int
)
go
insert into student
select ‘张三’,0,20,’内蒙赤峰’,’睡觉,聊天’,1 union
select ‘李颂’,1,19,’湖北襄樊’,’逛街’,2 union
select ‘吕两口’,0,18,’东北’,’轮滑’,3 union
select ‘小斌’,1,20,’湖北十堰’,’做菜’,3 union
select ‘哦哦’,0,19,’湖北枣阳’,’吃’,4 union
select ‘康康’,1,22,’河南南阳’,’看美女’,5 union
select ‘帅帅’,1,18,’湖北武汉’,’睡’,5 union
select ‘忽忽’,1,22,’陕西西安’,’游戏’,6 union
select ‘cc’,1,22,’湖北十堰’,’拼酒’,6 union
select ‘小剑’,0,18,’深圳’,’看MM’,7 union
select ‘勇勇’,1,19,’湖北十堰’,’吃了嘻嘻睡’,7
go
select * from classes
select * from student,classes where student.c_id=classes.id
select * from classes
html 文件:
班级
学生姓名
学生姓名 | 学生性别 | 学生年龄 | 学生住址 | 学生爱好 | 所在班级 |
---|
a.ashx 文件:
public class a : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = “text/plain”;
DataTable dt = getTable();
string str = JSONHelper.DataTableToJsonEx(dt);
context.Response.Write(str);
}
DataTable getTable()
{
return SqlHelper.GetTable(“select id,name from classes”);
}
public bool IsReusable
{
get
{
return false;
}
}
}
b.ashx 文件
public class b : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = “text/plain”;
int id = int.Parse(context.Request[“id”].ToString());
DataTable dt = getTable(id);
string ss = JSONHelper.DataTableToJsonEx(dt);
context.Response.Write(ss);
}
DataTable getTable(int id)
{
StringBuilder sb = new StringBuilder();
sb.Append(” select s.id as id,s.name as name,s.sex as sex, “);
sb.Append(” s.age as age,s.address as address, “);
sb.Append(” s.hobby as hobby,c.name as classname “);
sb.Append(” from student as s,classes as c “);
string sql = string.Empty;
sql = sb.ToString();
sql += string.Format(“where s.c_id=c.id and c.id={0}”,id);
// string sql = string.Format(“select * from student where c_id={0}”,id);
return SqlHelper.GetTable(sql);
}
public bool IsReusable
{
get
{
return false;
}
}
}
c.ashx 文件
public class c : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = “text/plain”;
int id = int.Parse(context.Request[“id”].ToString());
DataTable dt = getTable(id);
string ss = JSONHelper.DataTableToJsonEx(dt);
context.Response.Write(ss);
}
DataTable getTable(int id)
{
StringBuilder sb = new StringBuilder();
sb.Append(” select s.name as name,s.sex as sex,s.age as age, “);
sb.Append(” s.address as address,s.hobby as hobby, “);
sb.Append(” c.name as classname from student as s,classes as c “);
string sql = string.Empty;
sql = sb.ToString();
sql += string.Format(“where s.c_id=c.id and s.id={0}”,id);
// string sql = string.Format(“select s.name as name,s.sex as sex,s.age as age,s.address as address,s.hobby as hobby,c.name as classname from student as s,classes as c where s.c_id=c.id and s.id={0}”, id);
return SqlHelper.GetTable(sql);
}
public bool IsReusable
{
get
{
return false;
}
}
}
JSONHelper.cs 辅助文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
///
///JsonHelper 的摘要说明
///
public class JSONHelper
{
private static List
{
List
foreach (DataRow row in table.Rows)
{
Dictionary
foreach (DataColumn column in table.Columns)
{
dic.Add(column.ColumnName,row[column.ColumnName]);
}
list.Add(dic);
}
return list;
}
public static string ObjectToJson(object obj)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(obj);
}
public static string DataTableToJson(DataTable table)
{
return ObjectToJson(DataTableToList(table));
}
///
/// 通过拼字符串将DataTable转为Json
///
///
///
public static string DataTableToJsonEx(DataTable table)
{
string JsonName = “DataTable”;
StringBuilder Json = new StringBuilder(“{”” + JsonName + “”:[“);
if (table.Rows.Count > 0)
{
foreach (DataRow row in table.Rows)
{
Json.Append(“{“);
foreach (DataColumn column in table.Columns)
{
Json.Append(“”” + column.ColumnName + “”:”” + row[column.ColumnName].ToString() + “”,”);
}
Json.Remove(Json.Length – 1, 1);
Json.Append(“},”);
}
Json.Remove(Json.Length – 1, 1);
}
Json.Append(“]}”);
return Json.ToString();
}
}