您好,登錄后才能下訂單哦!
這篇文章給大家介紹HttpHandler如何進行增刪改查 ,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
如果不是asp.net webform,怎么進行增刪改查呢?
首先:
創建一個SqlHelper.cs
public class SqlHelper { public static readonly string conString = ConfigurationManager.ConnectionStrings["sqlCon"].ConnectionString; //增刪改 public static bool ExeNonQuery(string sql, CommandType type, params SqlParameter[] lists) { bool bFlag = false; using (SqlConnection con = new SqlConnection(conString)) { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = sql; cmd.CommandType = type; if (lists!=null) { foreach (SqlParameter p in lists) { cmd.Parameters.Add(p); } } try { if (con.State == ConnectionState.Closed) { con.Open(); } int result = cmd.ExecuteNonQuery(); if (result > 0) { bFlag = true; } } catch { ;} } return bFlag; } //查.讀 public static SqlDataReader ExeDataReader(string sql, CommandType type, params SqlParameter[] lists) { SqlConnection con = new SqlConnection(conString); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = sql; cmd.CommandType = type; if (con.State == ConnectionState.Closed) { con.Open(); } if (lists!=null) { foreach (SqlParameter p in lists) { cmd.Parameters.Add(p); } } SqlDataReader reader = cmd.ExecuteReader(); return reader; } //返回單個值 public static object GetScalar(string sql, CommandType type, params SqlParameter[] lists) { object returnValue = null; using (SqlConnection con = new SqlConnection(conString)) { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = sql; cmd.CommandType = type; if (lists!=null) { foreach (SqlParameter p in lists) { cmd.Parameters.Add(p); } } try { if (con.State == ConnectionState.Closed) { con.Open(); } returnValue = cmd.ExecuteScalar(); } catch { ; } } return returnValue; } //事務 public static bool ExeNonQueryTran(List<SqlCommand> list) { bool flag = true; SqlTransaction tran = null; using (SqlConnection con = new SqlConnection(conString)) { try { if (con.State == ConnectionState.Closed) { con.Open(); tran = con.BeginTransaction(); foreach (SqlCommand com in list) { com.Connection = con; com.Transaction = tran; com.ExecuteNonQuery(); } tran.Commit(); } } catch (Exception ex) { Console.Write(ex.Message); tran.Rollback(); flag = false; } } return flag; } //返回DataTable public static DataTable GetTable(string sql) { SqlConnection conn = new SqlConnection(conString); SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataTable table = new DataTable(); da.Fill(table); return table; } }
然后再Web.Config里面進行配置
<configuration> <connectionStrings> <add name="sqlCon" connectionString="server=.;database=student;uid=sa;pwd=fiybird"/> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web>
將要操作的表stus
創建一個html頁面List.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="JS/jquery-1.8.3.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("a:contains('刪除')").click(function () { if (!confirm("你確定要刪除么")) { return false; } }) }) </script> </head> <body> <table border="1" cellspacing="0" cellpadding="0" width="500"> <tr> <th>編號</th> <th>姓名</th> <th>性別</th> <th>班級</th> <th>操作(刪除)</th> <th>操作(查看)</th> <th>操作(修改)</th> </tr> {body}//這里是不是看不懂,先別急,往下看 </table> </body> </html> 接著創建一個一般處理程序List.ashx public class List : IHttpHandler { public void Proce***equest(HttpContext context) { context.Response.ContentType = "text/html"; // string path = context.Server.MapPath("List.html"); string strHtml = Commd.getFile("List.html"); context.Response.Write(strHtml.Replace("{body}", Getpost()));//把得到的數據和{body}進行替換 } string Getpost() { StringBuilder sb = new StringBuilder(); List<Stus> list = getAll(); foreach (Stus stu in list) { sb.AppendFormat("<tr>"); sb.AppendFormat("<td>" + stu.id + "</td>"); sb.AppendFormat("<td>"+stu.name+"</td>"); sb.AppendFormat("<td>" + stu.sex + "</td>"); sb.AppendFormat("<td>" + stu.c_id + "</td>"); sb.AppendFormat("<td><a href='Del.ashx?id={0}'>刪除</a></td>",stu.id); sb.AppendFormat("<td><a href='Dils.ashx?id={0}'>查看</a></td>", stu.id); sb.AppendFormat("<td><a href='Edit.ashx?id={0}'>修改</a></td>", stu.id); sb.AppendFormat("</tr>"); } return sb.ToString(); } public List<Stus> getAll()//查詢得到所有的數據 { string sql=string.Format("select * from stus"); List<Stus> list = new List<Stus>(); IDataReader red = SqlHelper.ExeDataReader(sql, CommandType.Text, null); while (red.Read()) { Stus s = new Stus { id = int.Parse(red[0].ToString()), name = red[1].ToString(), sex = red[2].ToString(), c_id = int.Parse(red[3].ToString()) }; list.Add(s); } return list; } public class Stus//創建類 { public int id { get; set; } public string name { get; set; } public string sex { get; set; } public int c_id { get; set; } } public bool IsReusable { get { return false; } } }
顯示效果:
接著進行刪除操作處理:
創建一個Del.ashx處理刪除
public class Del : IHttpHandler { public void Proce***equest(HttpContext context) { context.Response.ContentType = "text/html"; int id = int.Parse(context.Request["id"]); if(onDel(id)) { context.Response.Redirect("List.ashx"); } } bool onDel(int id) { string sql = string.Format("delete from stus where id={0}",id); return SqlHelper.ExeNonQuery(sql, CommandType.Text, null); } public bool IsReusable { get { return false; } } }
效果:
已經被刪除了
再接下來進行查看的操作,著里要創建一個Dils.ashx和Dils.html
先看 Dils.html頁面
<table border="1" cellspacing="0" cellpadding="0" width="300"> <tr> <th>編號</th> <th>姓名</th> <th>性別</th> <th>班級</th> </tr> <tr> <td>{id}</td> <td>{name}</td> <td>{sex}</td> <td>{c_id}</td> </tr> <tr> <td colspan="4"><a href="List.ashx">返回</a></td> </tr> </table>
然后Dils.ashx進行處理:
public class Dils : IHttpHandler { public void Proce***equest(HttpContext context) { context.Response.ContentType = "text/html"; int id = int.Parse(context.Request["id"]); string strHtml = Commd.getFile("Dils.html"); DataTable dt=getTable(id); strHtml = strHtml.Replace("{id}", dt.Rows[0]["id"].ToString()); strHtml=strHtml.Replace("{name}",dt.Rows[0]["name"].ToString()); strHtml=strHtml.Replace("{sex}",dt.Rows[0]["sex"].ToString()); strHtml = strHtml.Replace("{c_id}", dt.Rows[0]["c_id"].ToString()); context.Response.Write(strHtml); } DataTable getTable(int id) { string sql = string.Format("select id,name,sex,c_id from stus where id={0}",id); DataTable dt = SqlHelper.GetTable(sql); return dt; } public bool IsReusable { get { return false; } } }
效果:
最后來看看修改的操作:
這里先建立一個Edit.html頁面:
<form action="EditPros.ashx" method="post"> <table border="1" cellspacing="0" cellpadding="0" width="100%"> <tr> <th>編號</th> <th>姓名</th> <th>性別</th> <th>班級</th> </tr> <tr> <td><input type="txt" name="id" value="{id}" disabled="disabled" /> <input type="hidden" name="id" value="{id}" /> </td> <td><input type="txt" name="name" value="{name}" /></td> <td><input type="txt" name="sex" value="{sex}" /></td> <td><input type="txt" name="c_id" value="{c_id}" /></td> </tr> <tr> <td colspan="4"> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </td> </tr> </table> </form>
然后創建Edit.ashx和 EditPros.ashx來進行處理
Edit.ashx處理跟Dils.ashx差不多
public class Dils : IHttpHandler { public void Proce***equest(HttpContext context) { context.Response.ContentType = "text/html"; int id = int.Parse(context.Request["id"]); string strHtml = Commd.getFile("Dils.html"); DataTable dt=getTable(id); strHtml = strHtml.Replace("{id}", dt.Rows[0]["id"].ToString()); strHtml=strHtml.Replace("{name}",dt.Rows[0]["name"].ToString()); strHtml=strHtml.Replace("{sex}",dt.Rows[0]["sex"].ToString()); strHtml = strHtml.Replace("{c_id}", dt.Rows[0]["c_id"].ToString()); context.Response.Write(strHtml); } DataTable getTable(int id) { string sql = string.Format("select id,name,sex,c_id from stus where id={0}",id); DataTable dt = SqlHelper.GetTable(sql); return dt; } public bool IsReusable { get { return false; } } }
效果:
當點了提交后,要在 EditPros.ashx進行處理
public class EditPros : IHttpHandler { public void Proce***equest(HttpContext context) { context.Response.ContentType = "text/plain"; int id=int.Parse(context.Request["id"]); string name = context.Request["name"]; string sex = context.Request["sex"]; int c_id = int.Parse(context.Request["c_id"]); if (onUp(id, name, sex, c_id)) { context.Response.Redirect("List.ashx"); } } bool onUp(int id, string name, string sex, int c_id) { string sql = string.Format("update stus set name='{0}',sex='{1}',c_id={2} where id={3}", name, sex, c_id, id); return SqlHelper.ExeNonQuery(sql,CommandType.Text,null); } public bool IsReusable { get { return false; } } }
效果:
關于HttpHandler如何進行增刪改查 就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。