为了更好的表达与应用,方便初学者的学习,在此我写出四个一般处理程序,分别实现增、删、改、查,一通百通。
增:
add.ashx:
using System.Web;
using System.Data.SqlClient;
namespace test_SQLHelper_
{
/// <summary>
/// Add 的摘要说明
/// </summary>
public class Add : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string examsubject=context.Request["examsubject"];
string languageclass=context.Request["languageclass"];
SqlParameter[] para={
new SqlParameter("@ExamSubject",examsubject),
new SqlParameter("@LanguageClass",languageclass)
};
SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionStrings,System.Data.CommandType.Text,"insert into T_ExamCalendar (ExamSubject,LanguageClass) values(@ExamSubject,@LanguageClass)",para);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
删:
delete.ashx:
using System.Web;
using System.Data.SqlClient;
namespace test_SQLHelper_
{
/// <summary>
/// delete 的摘要说明
/// </summary>
public class delete : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string examsubject = context.Request["examsubject"];
SqlParameter[] pare = {
new SqlParameter ("@ExamSubject",examsubject)
};
SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionStrings,System.Data.CommandType.Text,"delete from T_ExamCalendar where ExamSubject=@ExamSubject",pare);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
改:
update.ashx:
using System.Web;
using System.Data.SqlClient;
namespace test_SQLHelper_
{
/// <summary>
/// update 的摘要说明
/// </summary>
public class update : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string examsubject = context.Request["examsubject"];
string examaccuratetime = context.Request["examaccuratetime"];
SqlParameter[] para = {
new SqlParameter ("@ExamSubject",examsubject),
new SqlParameter("@ExamAccurateTime",examaccuratetime)
};
SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionStrings,System.Data.CommandType.Text,"update T_ExamCalendar set ExamAccurateTime=@ExamAccurateTime where ExamSubject=@ExamSubject",para);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
查:
check.ashx:
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Web.Script.Serialization;
namespace test_SQLHelper_
{
/// <summary>
/// check 的摘要说明
/// </summary>
public class check : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
DataTable dt = new DataTable();
dt = SQLHelper.ExecuteDataTable(SQLHelper.ConnectionStrings, System.Data.CommandType.Text, "select * from T_ExamCalendar");
List<Dictionary<string, string>> List = new List<Dictionary<string, string>>();
for (int i = 0; i < dt.Rows.Count;i++ ) {
var Dic = new Dictionary<string, string> {
{"ExamSubject",dt.Rows[i]["ExamSubject"].ToString()},
{"LanguageClass",dt.Rows[i]["LanguageClass"].ToString()}
};
List.Add(Dic);
}
JavaScriptSerializer jss = new JavaScriptSerializer();
context.Response.Write(jss.Serialize(List));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
使用的连接字符串为:(写成SQLHelper的一个字段)
public static readonly string ConnectionStrings = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
于是每次都如此调用,如上:Check.ashx中的(粗体部分为链接字符串,写在web.config中)
dt = SQLHelper.ExecuteDataTable(SQLHelper.ConnectionStrings, System.Data.CommandType.Text, "select * from T_ExamCalendar");
web.config中的链接字符串如下:
<connectionStrings>
<add name="ConnectionStrings" connectionString="server=CHENSHENGTAI-PC;database=EXAMCALENDAR.MDF;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>