说明(2017-7-4 11:48:50):

1. index.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<a href="Add.html"><input type="button" name="btnAdd" value="添加用户" /></a> <table border="" cellpadding="" cellspacing="">
<tr>
<th>
ID
</th>
<th>
用户名
</th>
<th>
密码
</th>
<th>
修改
</th>
<th>
删除
</th>
</tr>
$tbody
</table>
</body>
<script type="text/javascript" src="js/jquery2.1.4.js"></script>
<script type="text/javascript">
$("a:contains(删除)").click(function () {
var isDel = confirm("是否删除?");
if (isDel == false) {
return false;
}
})
</script>
</html>

2. index.ashx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Data; namespace Itcast.WebApp
{
/// <summary>
/// index 的摘要说明
/// </summary>
public class index : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//context.Response.Write("Hello World");
string path = context.Request.MapPath("index.html");
string strHtml = File.ReadAllText(path);
StringBuilder sb = new StringBuilder(); string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
string sql = "select * from UserList";
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
{ DataTable dt = new DataTable();
sda.Fill(dt);
for (int i = ; i < dt.Rows.Count; i++)
{
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td><a href = 'Edit.ashx?id={0}'>修改</a></td><td><a href='Delete.ashx?id={0}'>删除</a></td></tr>", dt.Rows[i]["UserId"].ToString(), dt.Rows[i]["UserName"].ToString(), dt.Rows[i]["UserPwd"]);
} }
}
strHtml = strHtml.Replace("$tbody", sb.ToString());
context.Response.Write(strHtml);
} public bool IsReusable
{
get
{
return false;
}
}
}
}

3. web.config

 <?xml version="1.0" encoding="utf-8"?>

 <!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<!--Data Source=JJW-LENOVO\SQLEXPRESS;Initial Catalog=jjwdb;Integrated Security=True-->
<add connectionString="Data Source =.; Initial Catalog = jjwdb;Integrated Security = True" name="conStr"/>
</connectionStrings>
</configuration>

4. add.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="Add.ashx" method="post">
<table border="" cellpadding="" cellspacing="">
<tr>
<td>
用户名
</td>
<td>
<input type="text" name="UserName" value="" />
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<input type="text" name="UserPwd" value="" />
</td>
</tr>
<tr>
<td>
<input type="submit" name="name" value="提交" /></td>
</tr>
</table>
</form>
</body>
</html>

5. add.ashx

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; namespace Itcast.WebApp
{
/// <summary>
/// Add 的摘要说明
/// </summary>
public class Add : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string UserName = context.Request.Form["UserName"];
string UserPwd = context.Request.Form["UserPwd"];
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
string sql = "insert into UserList(UserName,UserPwd) values(@UserName, @UserPwd)";
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddWithValue("@UserName",UserName);
cmd.Parameters.AddWithValue("@UserPwd",UserPwd);
cmd.ExecuteNonQuery();
}
}
context.Response.Redirect("index.ashx");
} public bool IsReusable
{
get
{
return false;
}
}
}
}

6. delete.ashx

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; namespace Itcast.WebApp
{
/// <summary>
/// Delete 的摘要说明
/// </summary>
public class Delete : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
string sql = "delete from UserList where UserId = @UserId";
string UserId = context.Request.QueryString["id"];
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddWithValue("@UserId",UserId);
cmd.ExecuteNonQuery();
}
}
context.Response.Redirect("index.ashx");
} public bool IsReusable
{
get
{
return false;
}
}
}
}

7. edit.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="Edit2.ashx" method="post">
<input type="hidden" name="UserId" value="$UserId" />
用户名<input type="text" name="UserName" value="$UserName" /></br>
密码<input type="text" name="UserPwd" value="$UserPwd" /></br>
<input type="submit" name="name" value="保存" />
</form>
</body>
</html>

8. edit.ashx

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Data;
using System.IO; namespace Itcast.WebApp
{
/// <summary>
/// Edit 的摘要说明
/// </summary>
public class Edit : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//context.Response.Write("Hello World");
string path = context.Request.MapPath("Edit.html");
string strHtml = File.ReadAllText(path);
string UserId = context.Request.QueryString["id"];
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
string sql = "select * from UserList where UserId = @UserId";
string UserName = null;
string UserPwd = null;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(sql,con))
{
sda.SelectCommand.Parameters.AddWithValue("@UserId",UserId);
DataTable dt = new DataTable();
sda.Fill(dt);
UserName = dt.Rows[]["UserName"].ToString();
UserPwd = dt.Rows[]["UserPwd"].ToString(); }
}
strHtml = strHtml.Replace("$UserId",UserId).Replace("$UserName",UserName).Replace("$UserPwd",UserPwd);
context.Response.Write(strHtml);
} public bool IsReusable
{
get
{
return false;
}
}
}
}

9. edit2.ashx

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; namespace Itcast.WebApp
{
/// <summary>
/// Edit2 的摘要说明
/// </summary>
public class Edit2 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
string sql = "update UserList set UserName = @UserName, UserPwd = @UserPwd where UserId = @UserId";
string UserId = context.Request.Form["UserId"];
string UserName = context.Request.Form["UserName"];
string UserPwd = context.Request.Form["UserPwd"];
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddWithValue("@UserId", UserId);
cmd.Parameters.AddWithValue("@UserName", UserName);
cmd.Parameters.AddWithValue("@UserPwd", UserPwd);
cmd.ExecuteNonQuery();
}
}
context.Response.Redirect("index.ashx");
} public bool IsReusable
{
get
{
return false;
}
}
}
}

ASP.NET学习笔记(2)——用户增删改查的更多相关文章

  1. EF学习笔记-1 EF增删改查

    首次接触Entity FrameWork,就感觉非常棒.它节省了我们以前写SQL语句的过程,同时也让我们更加的理解面向对象的编程思想.最近学习了EF的增删改查的过程,下面给大家分享使用EF对增删改查时 ...

  2. HTML5+ 学习笔记3 storage.增删改查

    //插入N条数据 function setItemFun( id ) { //循环插入100调数据 var dataNum = new Number(id); for ( var i=0; i< ...

  3. 【JAVAWEB学习笔记】20_增删改查

    今天主要是利用三层架构操作数据库进行增删查改操作. 主要是编写代码为主. 附图: 前台和后台 商品的展示 修改商品

  4. Python学习笔记-列表的增删改查

  5. [学习笔记] Oracle基础增删改查用法

    查询 select *|列名|表达式 from 表名 where 条件 order by 列名 select t.* from STUDENT.STUINFO t where t.stuname = ...

  6. python学习之-成员信息增删改查

    python学习之-成员信息增删改查 主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env p ...

  7. 前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查

    AngularJS中的$resource服务相比$http服务更适合与RESTful服务进行交互.本篇后端使用ASP.NET Web API, 前端使用$resource,实现增删改查. 本系列包括: ...

  8. 使用HttpClient对ASP.NET Web API服务实现增删改查

    本篇体验使用HttpClient对ASP.NET Web API服务实现增删改查. 创建ASP.NET Web API项目 新建项目,选择"ASP.NET MVC 4 Web应用程序&quo ...

  9. 第二百七十六节,MySQL数据库,【显示、创建、选定、删除数据库】,【用户管理、对用户增删改查以及授权】

    MySQL数据库,[显示.创建.选定.删除数据库],[用户管理.对用户增删改查以及授权] 1.显示数据库 SHOW DATABASES;显示数据库 SHOW DATABASES; mysql - 用户 ...

  10. 用户增删改查 django生命周期 数据库操作

    一 django生命周期 1 浏览器输入一个请求(get/post)2 响应到django程序中3 执行到url,url通过请求的地址匹配到不同的视图函数4 执行对应的视图函数,此过程可以查询数据库, ...

随机推荐

  1. hadoop资料

    1."Hadoop.Operations.pdf.zip" http://vdisk.weibo.com/s/vDOQs6xMAQH6 2."Hadoop权威指南(中文版 ...

  2. SQL plan directives

    SQL plan directives SQL plan directives含有优化器产生优化的执行计划时需要的附加信息和指令. 在sql执行时,如果cardinality估计有错误,数据库就会创建 ...

  3. 12C -- 配置EM Express的端口

    EM Express是基于web接口的图形化数据库管理工具. 内嵌到数据库中,可以用来监控.管理数据的性能和完成大多数管理工作. EM Express是轻量级的管理工具,减少了数据库服务器的开销.没有 ...

  4. CentOS安装OpenResty(Nginx+Lua)开发环境

    一.简介 OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高 ...

  5. 重新认识Javascript的一些误区总结

    1.在函数内有没有var真的不一样 下面这样一段代码,在函数abc()中,创建了两个变量a, c,并在函数体之外进行alert,想看看有什么事发生: <script> function a ...

  6. Create rolling monthly, weekly and daily Logstash indices

    在刚刚开始接触ELK的时候我们习惯把每一个index都按照day来切割.但是我们会发现我们的shards 会很多. 其实我们一该把那些小的index按照一周或者一个月来rolling,来减少我们的sh ...

  7. php分享十八七:mysql基础

    mysql操作数据库代码: $link = @mysql_connect('localhost:3306', 'root', 'root') or die(mysql_error()); mysql_ ...

  8. [na]数据链路层&网络层协议小结截图版

    ip层:分组选路 tcp:端到端的通信 中间系统没必要有应用程序,分组选路即可 应用程序中隐藏所有的物理细节. 语音肯定用udp linux主版本 次版本号 修订号 次版本为偶数说明是稳定版.奇数是开 ...

  9. Merge into 详细介绍

    /*Merge into 详细介绍 MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句. 通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询, 连接条 ...

  10. schema中字段类型的定义

    当schema中字段类型为String时,保存的时候如果该字段为Number也可以保存成功,mongoose会自动将其转换为数字字符串. 当schema中字段类型为Number时,保存的时候如果该字段 ...