Register.html
1 2 3 4 581 826 7 68 69 70
Register.ashx
1 public class Register : IHttpHandler 2 { 3 4 public void ProcessRequest(HttpContext context) 5 { 6 context.Response.ContentType = "text/plain"; 7 8 //操作类型:1判断用户名是否存在,2进行注册insert 9 string action = context.Request["action"];10 if (action.Equals("1"))11 {12 string sql = "select count(*) from userinfo where username=@name";13 SqlParameter p = new SqlParameter("@name", context.Request["uname"]);14 15 int obj = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, p));16 context.Response.Write(obj);17 18 }19 else if (action.Equals("2")) //注册20 {21 string sql = "insert into userinfo values(@name,@pwd)";22 SqlParameter[] ps =23 {24 new SqlParameter("@name",context.Request["uname"]),25 new SqlParameter("@pwd",context.Request["upwd"])26 };27 28 int result = SqlHelper.ExecuteNonQuery(sql, ps);29 context.Response.Write(result);30 }31 32 33 }34 35 public bool IsReusable36 {37 get38 {39 return false;40 }41 }42 }
Login.html
1 2 3 4 536 376 7 26 27 28
Login.ashx
1 public class Login : IHttpHandler 2 { 3 4 public void ProcessRequest(HttpContext context) 5 { 6 context.Response.ContentType = "text/plain"; 7 int result = 1;//1成功,2用户名错,3密码错 8 9 string uname = context.Request["uname"];10 string upwd = context.Request["upwd"];11 12 string sql = "select userpwd from userinfo where username=@name";13 SqlParameter p = new SqlParameter("@name", uname);14 15 object upwd2 = SqlHelper.ExecuteScalar(sql, p);16 if (upwd2 == null)17 {18 result = 2;19 }20 else if (upwd2.ToString().Equals(upwd))21 {22 result = 1;23 }24 else25 {26 result = 3;27 }28 29 context.Response.Write(result);30 }31 32 public bool IsReusable33 {34 get35 {36 return false;37 }38 }39 }
SqlHelper.cs
1 public static class SqlHelper 2 { 3 private static string connStr = 4 System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString; 5 6 public static DataTable GetList(string sql, params SqlParameter[] ps) 7 { 8 using (SqlConnection conn = new SqlConnection(connStr)) 9 {10 SqlDataAdapter sda = new SqlDataAdapter(sql, conn);11 sda.SelectCommand.Parameters.AddRange(ps);12 13 DataTable dt = new DataTable();14 sda.Fill(dt);15 16 return dt;17 }18 }19 20 public static int ExecuteNonQuery(string sql, params SqlParameter[] ps)21 {22 using (SqlConnection conn = new SqlConnection(connStr))23 {24 SqlCommand cmd = new SqlCommand(sql, conn);25 cmd.Parameters.AddRange(ps);26 27 conn.Open();28 return cmd.ExecuteNonQuery();29 }30 }31 32 public static object ExecuteScalar(string sql, params SqlParameter[] ps)33 {34 using (SqlConnection conn = new SqlConnection(connStr))35 {36 SqlCommand cmd = new SqlCommand(sql, conn);37 cmd.Parameters.AddRange(ps);38 39 conn.Open();40 return cmd.ExecuteScalar();41 }42 }43 44 }
Web.config
1 2 3 7 89 10 1311 12 14 1615