好乱的代码

<%@ 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;
}
}

}

posted @ 2014-04-30 14:44  城小镇  阅读(173)  评论(0编辑  收藏  举报