Foreground code unchanged, the background C # code needs to build a class page display effect is not changed
Using system;using system.collections.generic;using system.linq;using system.web;using System.Data;using system.data.sqlclient;///<summary>///DB Summary description///</summary>public class Db{public db () {////TODO: Add constructor logic here//} public string SqlDB (string database, String type, String sql) {string connstring = "server=.; Database= "+ database +"; uid=sa;pwd=123 "; SqlConnection conn = new SqlConnection (connstring); SqlCommand cmd = conn. CreateCommand (); Cmd.commandtext = SQL; String Shuju = ""; Conn. Open (); if (type = = "CX") {SqlDataReader _dr = cmd. ExecuteReader (); while (_DR. Read ()) {for (int i = 0; i < _DR. FieldCount; i++) {Shuju + = _dr[i]. ToString () + "^"; } Shuju = Shuju. Substring (0, Shuju. LENGTH-1); Shuju + = "|"; } Shuju = Shuju. Substring (0, Shuju. LENGTH-1); } else {int a = cmd. ExecuteNonQuery (); if (a > 0) {Shuju = "Success!" "; } else {Shuju = "Failed! "; }} conn. Close (); return Shuju; }}
The Select page will then change:
Using system.data;using System.data.sqlclient;public class Select:ihttphandler {public void ProcessRequest ( HttpContext context) { //Fetch value string name = Context. request["Name"]. ToString (); Use the method of the class //Make an SQL statement string sql = "Select *from Info where name like '%" + name + "% '"; function db cmd = new db () inside the DB class; The function requires three parameters to return a string of Shuju = cmd. SqlDB ("MyDB", "CX", SQL); Write the string out of context. Response.Write (Shuju); Context. Response.End (); } public bool IsReusable { get { return false; } }
WB AJax Example 2 the second method