说明(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. OpenCV 学习笔记 02 处理文件、摄像头和图形用户界面

    在处理文件前需要引入OpenCV库,同时也引入unmpy库 import cv2 import numpy as np 1 基本的读写操作 1.1 图像文件的读写操作 1.1.1 图像文件的读取操作 ...

  2. ORACLE在线切换undo表空间

    切换undo的一些步骤和基本原则 原文:http://www.xifenfei.com/3367.html 查看原undo相关参数 SHOW PARAMETER UNDO; 创建新undo空间 cre ...

  3. [Android] 使用Include布局+Fragment滑动切换屏幕

        前面的文章已经讲述了"随手拍"项目图像处理的技术部分,该篇文章主要是主界面的布局及屏幕滑动切换,并结合鸿洋大神的视频和郭神的第一行代码(强推两人Android博客),完毕了 ...

  4. 使用mysqldump导入导出MySQL数据库

    数据库的基本导入\导出的命令 是 mysqldump 和 source 在linux下直接用命令行操作就可以 在windows下 一般情况下有两种方法一个也是用命令行 另一个是用phpmyadmin ...

  5. pyspark 随机森林特征重要性

    # IMPORT >>> import numpy >>> from numpy import allclose >>> from pyspark ...

  6. springboot 多环境配置yml或properties

    https://www.cnblogs.com/mr-yang-localhost/p/8971327.html   springboot 多环境配置 https://blog.csdn.net/li ...

  7. Sql 查询当天、本周、本月记录

    --查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: select * from info wh ...

  8. 引文分析工具HistCite使用简介

    运行环境: win8.1(lenovo Y450) 1.去www.histcite.com下载histcite最新版,并安装 2.去WOS下载文献.保存方式为: 记录数: 记录1至500(最大支持50 ...

  9. Windows搭建测试RabbitMq遇到的问题

    报错: d:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.5\sbin>rabbitmq-plugins eble rabbitmq_ma ...

  10. WPF对象级资源的定义与查找

    文章概述: 本演示介绍了怎样定义WPF对象级的资源,并通过XAML代码和C#訪问和使用对象级资源. 相关下载(代码.屏幕录像):http://pan.baidu.com/s/1hqvJNY8 在线播放 ...