In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to add, delete, modify and check HttpHandler. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
If it is not asp.net webform, how to add, delete, modify and check?
First of all:
Create a SqlHelper.cs
Public class SqlHelper {public static readonly string conString = ConfigurationManager.ConnectionStrings ["sqlCon"] .ConnectionString; / / add, delete, modify 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 (listslists 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 Read 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 (listslists null) {foreach (SqlParameter p in lists) {cmd.Parameters.Add (p) }} SqlDataReader reader = cmd.ExecuteReader (); return reader;} / / returns a single value 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 (listslists null) {foreach (SqlParameter p in lists) {cmd.Parameters.Add (p) }} try {if (con.State = = ConnectionState.Closed) {con.Open ();} returnValue = cmd.ExecuteScalar () } catch {;}} return returnValue;} / / transaction public static bool ExeNonQueryTran (List 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 } / / return 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 }}
Then configure it in Web.Config.
Table stus to be operated on
Create a html page List.html
(function () {$("a:contains ('delete')) .click (function () {if (! confirm (" are you sure you want to delete ") {return false }}) number name, gender class operation (delete) operation (view) operation (modify) {body} / / Don't worry if you don't understand here. Read on and create a general handler List.ashxpublic 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 ()); / / replace the resulting data with {body}} string Getpost () {StringBuilder sb = new StringBuilder () List list = getAll (); foreach (Stus stu in list) {sb.AppendFormat ("); sb.AppendFormat (" + stu.id + "); sb.AppendFormat ("+ stu.name+"); sb.AppendFormat ("" + stu.sex + "") Sb.AppendFormat ("+ stu.c_id +"); sb.AppendFormat ("delete", stu.id); sb.AppendFormat ("View", stu.id); sb.AppendFormat ("modify", stu.id); sb.AppendFormat ("");} return sb.ToString () } public List getAll () / / query all the data {string sql=string.Format ("select * from stus"); List list = new List (); 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// create class {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;}
Display effect:
The deletion operation is then processed:
Create a Del.ashx to handle deletion
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 }}}
Effect:
Has been deleted.
Next, to check, create a Dils.ashx and Dils.html.
Take a look at the Dils.html page first.
Number, name, gender, class {id} {name} {sex} {c_id} returned
Then Dils.ashx processes:
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;}
Effect:
Finally, let's take a look at the modified operation:
Let's first create an Edit.html page:
Number, name, gender, class.
Then create Edit.ashx and EditPros.ashx for processing
Edit.ashx processing is similar to 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;}
Effect:
When you click submit, you need to process it in 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}', clockid = {2} where id= {3}", name, sex, c_id, id); return SqlHelper.ExeNonQuery (sql,CommandType.Text,null) } public bool IsReusable {get {return false;}
Effect:
On how to add, delete, modify and check HttpHandler to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 278
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.