说明(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. webpack window 安装loader

    1.安装loadernpm install css-loader style-loader --save-dev 2.配置loader,在webpack.config.js中 module: { lo ...

  2. magento的一些小技巧(转)

    1.加载某个attribute: $attributeCode=Mage::getModel('catalog/resource_eav_attribute')                    ...

  3. Entity Framework 无法加载指定的元数据资源。

    ADO.NET Entity Framework发布以来,本人也一直在用,深感好用,忍不住地要感谢微软啊!由于项目结构创建完成后,没怎么改动过,所以一直没出题过问题,可最近由于改动了下命名空间,问题来 ...

  4. PowerDesigner使用:[3]创建索引

    PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...

  5. 【php】thinkphp以post方式查询时分页失效的解决方法

    好久没有写博客了,最近说实话有点忙,各个项目都需要改bug.昨天晚上一直没有解决的php项目中的bug,就在刚才终于搞定,在这里还需要感谢博客园大神给的帮助! 具体问题描述 最近遇到一个非常棘手的问题 ...

  6. 【转】对 Parser 的误解

    一直很了解人们对于parser的误解,可是一直都提不起兴趣来阐述对它的观点.然而我觉得是有必要解释一下这个问题的时候了.我感觉得到大部分人对于parser的误解之深,再不澄清一下,恐怕这些谬误就要写进 ...

  7. C++项目參考解答:求Fibonacci数列

    [项目:求Fibonacci数列] Fibonacci数列在计算科学.经济学等领域中广泛使用,其特点是:第一.二个数是1,从第3个数開始,每一个数是其前两个数之和.据此,这个数列为:1 1 2 3 5 ...

  8. Win7 64bit 安装VisualSVN出现报错:Servic &#39;VisualSVN Server&#39; failed to start.解决的方法

    问题描写叙述: Win7 64bit 安装VisualSVN时出现报错: Servic 'VisualSVN Server' failed to start.Please check VisualSV ...

  9. MySQL update 语句与标准SQL不同的地方

    [SQL标准中有一个叫同时执行的概念] 同时执行指的是在同一个子句中的各个部分的执行时机是不区分先后的,如下面的SQL语句 ),); +---------+--------+ ) ) | +----- ...

  10. windows如何查看某个端口被谁占用

    我们在启动应用的时候经常发现我们需要使用的端口被别的程序占用,但是我们又不知道是被谁占用,这时候我们需要找出“真凶”,如何做到呢? cmd命令中 输入命令:netstat -ano,列出所有端口的情况 ...