using Model;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Linq;
using System.Web;
namespace Mywall.ashx
{
/// <summary>
/// _default 的摘要说明
/// </summary>
public class _default : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
//建立数据库连接
ConnectionString = "Data Source=.;Initial Catalog=SqlSugarTest;Integrated Security=True",
DbType = DbType.SqlServer,//设置数据库类型
IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
});
//用来打印Sql方便你调式
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" +
db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine();
};
var list = db.Queryable<T_student>().ToList(); //查询所有的数据
context.Response.Write(JsonConvert.SerializeObject(list));
var getByWhere = db.Queryable<T_student>().Where(it => it.ID == 1).ToList();//根据条件查询
context.Response.Write(JsonConvert.SerializeObject(getByWhere));
var total = 0;
var getPage = db.Queryable<T_student>().Where(it => it.ID == 1).ToPageList(1, 5, ref total);//根据分页进行查询
context.Response.Write(JsonConvert.SerializeObject(getPage));
//执行新增
var datas = new T_student() { name = "贾宝玉", sex = "女", sch_id = 1001, isOk = true };
db.Insertable(datas).InsertColumns(it => new { it.name, it.sex, it.sch_id, it.isOk }).ExecuteReturnIdentity();
var data = new T_student() { name = "思齐", sex = "女", sch_id = 1000, isOk = false };
db.Insertable(data).InsertColumns(it => new { it.name, it.sex, it.sch_id, it.isOk }).ExecuteReturnIdentity();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
网友评论