好乱的代码

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

using System;
using System.Web;

public class Search_Book : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";


string txtTitle = context.Request["txtTitle"];
string txtAuthor = context.Request["txtAuthor"];
string txtPress = context.Request["txtPress"];

string sql = @"select * from Item where 1=1 ";
System.Data.DataTable dt;


if (!string.IsNullOrEmpty(txtTitle))
{
sql += @" and ItemName like '%" + @txtTitle + "%' ";

}

if (!string.IsNullOrEmpty(txtAuthor))
{
sql += @" and BookAuthor like '%" + @txtAuthor + "%' ";
}
if (!string.IsNullOrEmpty(txtPress))
{
sql += @" and publisher like '%" + @txtPress + "%' ";

}

if (!string.IsNullOrEmpty(txtTitle))
{
dt = SqlHelper.ExecuteDataTable(sql, new System.Data.SqlClient.SqlParameter(@txtTitle, txtTitle));

}
else if (!string.IsNullOrEmpty(txtAuthor))
{
dt = SqlHelper.ExecuteDataTable(sql, new System.Data.SqlClient.SqlParameter(@txtAuthor, txtAuthor));
}
else if (!string.IsNullOrEmpty(txtPress))
{
dt = SqlHelper.ExecuteDataTable(sql, new System.Data.SqlClient.SqlParameter(@txtPress, txtPress));
}
else if (!string.IsNullOrEmpty(txtTitle) && !string.IsNullOrEmpty(txtAuthor))
{
dt = SqlHelper.ExecuteDataTable(sql, new System.Data.SqlClient.SqlParameter(@txtTitle, txtTitle), new System.Data.SqlClient.SqlParameter(@txtAuthor, txtAuthor));
}
else if (!string.IsNullOrEmpty(txtTitle) && !string.IsNullOrEmpty(txtPress))
{
dt = SqlHelper.ExecuteDataTable(sql, new System.Data.SqlClient.SqlParameter(@txtTitle, txtTitle), new System.Data.SqlClient.SqlParameter(@txtPress, txtPress));

}
else if (!string.IsNullOrEmpty(txtAuthor) && !string.IsNullOrEmpty(txtPress))
{
dt = SqlHelper.ExecuteDataTable(sql, new System.Data.SqlClient.SqlParameter(@txtAuthor, txtAuthor), new System.Data.SqlClient.SqlParameter(@txtPress, txtPress));
}
else if (!string.IsNullOrEmpty(txtTitle) && !string.IsNullOrEmpty(txtAuthor) && !string.IsNullOrEmpty(txtPress))
{
dt = SqlHelper.ExecuteDataTable(sql, new System.Data.SqlClient.SqlParameter(@txtTitle, txtTitle), new System.Data.SqlClient.SqlParameter(@txtAuthor, txtAuthor), new System.Data.SqlClient.SqlParameter(@txtPress, txtPress));

}

dt = SqlHelper.ExecuteDataTable(sql);


if (dt.Rows.Count > 0)
{
// lblShow.Text = "";

//object[] datas = new object[dt.Rows.Count];
//for (int i = 0; i < datas.Length; i++)
//{
// System.Data.DataRow row = dt.Rows[i];
// DateTime addDT = (DateTime)row["AddTime"];
// datas[i] = new { BookId = row["BookId"], BookAuthor = row["BookAuthor"], Publisher = row["Publisher"], PublishDate = addDT.ToString(), BookPrice = row["BookPrice"], ItemName = row["ItemName"], ItemImage = row["ItemImage"] };
//}, ListPrice = row["ListPrice"]
//string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(datas);

 

object[] comments = new object[dt.Rows.Count];
for (int i = 0; i < comments.Length; i++)
{
System.Data.DataRow row = dt.Rows[i];
DateTime creatDT = (DateTime)row["PublishDate"];
comments[i] = new { BookId = row["BookId"], BookAuthor = row["BookAuthor"], Publisher = row["Publisher"], PublishDate = creatDT.ToString("yyyy-MM-dd"), img = row["ItemImage"], ListPrice = row["ListPrice"], ItemName = row["ItemName"] };
}
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(comments);


context.Response.Write(json);


}
else if (dt.Rows.Count <= 0)
{

context.Response.Write("error_1");
}

 

}

public bool IsReusable {
get {
return false;
}
}

}

转载于:https://www.cnblogs.com/cxzbk/p/3701304.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值