• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET学习笔记(2)——用户增删改查

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

1. index.html

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title></title>
 5 </head>
 6 <body>
 7     <a href="Add.html"><input type="button" name="btnAdd" value="添加用户" /></a>
 8     
 9     <table border="1" cellpadding="0" cellspacing="0">
10         <tr>
11             <th>
12                 ID
13             </th>
14             <th>
15                 用户名
16             </th>
17             <th>
18                 密码
19             </th>
20             <th>
21                 修改
22             </th>
23             <th>
24                 删除
25             </th>
26         </tr>
27         $tbody
28     </table>
29 </body>
30 <script type="text/javascript" src="js/jquery2.1.4.js"></script>
31 <script type="text/javascript">
32     $("a:contains(删除)").click(function () {
33         var isDel = confirm("是否删除?");
34         if (isDel == false) {
35             return false;
36         }
37     })
38 </script>
39 </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 = 0; 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

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <!--
 4   有关如何配置 ASP.NET 应用程序的详细消息,请访问
 5   http://go.microsoft.com/fwlink/?LinkId=169433
 6   -->
 7 
 8 <configuration>
 9     <system.web>
10         <compilation debug="true" targetFramework="4.0" />
11     </system.web>
12   <connectionStrings>
13     <!--Data Source=JJW-LENOVO\SQLEXPRESS;Initial Catalog=jjwdb;Integrated Security=True-->
14     <add connectionString="Data Source =.; Initial Catalog = jjwdb;Integrated Security = True" name="conStr"/>  
15   </connectionStrings>
16 </configuration>

4. add.html

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title></title>
 5 </head>
 6 <body>
 7     <form action="Add.ashx" method="post">
 8     <table border="0" cellpadding="0" cellspacing="0">
 9         <tr>
10             <td>
11                 用户名
12             </td>
13             <td>
14                 <input type="text" name="UserName" value="" />
15             </td>
16         </tr>
17          <tr>
18             <td>
19                 密码
20             </td>
21             <td>
22                 <input type="text" name="UserPwd" value="" />
23             </td>
24         </tr>
25         <tr>
26             <td>
27                 <input type="submit" name="name" value="提交" /></td>
28         </tr>
29     </table>
30     </form>
31 </body>
32 </html>

5. add.ashx

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Data;
 6 using System.Data.SqlClient;
 7 using System.Configuration;
 8 
 9 namespace Itcast.WebApp
10 {
11     /// <summary>
12     /// Add 的摘要说明
13     /// </summary>
14     public class Add : IHttpHandler
15     {
16 
17         public void ProcessRequest(HttpContext context)
18         {
19             context.Response.ContentType = "text/plain";
20             string UserName = context.Request.Form["UserName"];
21             string UserPwd = context.Request.Form["UserPwd"];
22             string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
23             string sql = "insert into UserList(UserName,UserPwd) values(@UserName, @UserPwd)";
24             using (SqlConnection con = new SqlConnection(conStr))
25             {
26                 using (SqlCommand cmd = new SqlCommand(sql,con))
27                 {
28                     con.Open();
29                     cmd.Parameters.AddWithValue("@UserName",UserName);
30                     cmd.Parameters.AddWithValue("@UserPwd",UserPwd);
31                     cmd.ExecuteNonQuery();
32                 }
33             }
34             context.Response.Redirect("index.ashx");
35         }
36 
37         public bool IsReusable
38         {
39             get
40             {
41                 return false;
42             }
43         }
44     }
45 }

6. delete.ashx

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Data;
 6 using System.Data.SqlClient;
 7 using System.Configuration;
 8 
 9 namespace Itcast.WebApp
10 {
11     /// <summary>
12     /// Delete 的摘要说明
13     /// </summary>
14     public class Delete : IHttpHandler
15     {
16 
17         public void ProcessRequest(HttpContext context)
18         {
19             context.Response.ContentType = "text/plain";
20             string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
21             string sql = "delete from UserList where UserId = @UserId";
22             string UserId = context.Request.QueryString["id"];
23             using (SqlConnection con = new SqlConnection(conStr))
24             {
25                 using (SqlCommand cmd = new SqlCommand(sql,con))
26                 {
27                     con.Open();
28                     cmd.Parameters.AddWithValue("@UserId",UserId);
29                     cmd.ExecuteNonQuery();
30                 }
31             }
32             context.Response.Redirect("index.ashx");
33         }
34 
35         public bool IsReusable
36         {
37             get
38             {
39                 return false;
40             }
41         }
42     }
43 }

7. edit.html

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

8. edit.ashx

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Data.SqlClient;
 6 using System.Configuration;
 7 using System.Text;
 8 using System.Data;
 9 using System.IO;
10 
11 namespace Itcast.WebApp
12 {
13     /// <summary>
14     /// Edit 的摘要说明
15     /// </summary>
16     public class Edit : IHttpHandler
17     {
18 
19         public void ProcessRequest(HttpContext context)
20         {
21             context.Response.ContentType = "text/html";
22             //context.Response.Write("Hello World");
23             string path = context.Request.MapPath("Edit.html");
24             string strHtml = File.ReadAllText(path);
25             string UserId = context.Request.QueryString["id"];
26             string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
27             string sql = "select * from UserList where UserId = @UserId";
28             string UserName = null;
29             string UserPwd = null;
30             using (SqlConnection con = new SqlConnection(conStr))
31             {
32                 using (SqlDataAdapter sda = new SqlDataAdapter(sql,con))
33                 {
34                     sda.SelectCommand.Parameters.AddWithValue("@UserId",UserId);
35                     DataTable dt = new DataTable();
36                     sda.Fill(dt);
37                     UserName = dt.Rows[0]["UserName"].ToString();
38                     UserPwd = dt.Rows[0]["UserPwd"].ToString();
39 
40                 }
41             }
42             strHtml = strHtml.Replace("$UserId",UserId).Replace("$UserName",UserName).Replace("$UserPwd",UserPwd);
43             context.Response.Write(strHtml);
44         }
45 
46         public bool IsReusable
47         {
48             get
49             {
50                 return false;
51             }
52         }
53     }
54 }

9. edit2.ashx

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Data;
 6 using System.Data.SqlClient;
 7 using System.Configuration;
 8 
 9 
10 namespace Itcast.WebApp
11 {
12     /// <summary>
13     /// Edit2 的摘要说明
14     /// </summary>
15     public class Edit2 : IHttpHandler
16     {
17 
18         public void ProcessRequest(HttpContext context)
19         {
20             context.Response.ContentType = "text/plain";
21             //context.Response.Write("Hello World");
22             string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
23             string sql = "update UserList set UserName = @UserName, UserPwd = @UserPwd where UserId = @UserId";
24             string UserId = context.Request.Form["UserId"];
25             string UserName = context.Request.Form["UserName"];
26             string UserPwd = context.Request.Form["UserPwd"];
27             using (SqlConnection con = new SqlConnection(conStr))
28             {
29                 using (SqlCommand cmd = new SqlCommand(sql,con))
30                 {
31                     con.Open();
32                     cmd.Parameters.AddWithValue("@UserId", UserId);
33                     cmd.Parameters.AddWithValue("@UserName", UserName);
34                     cmd.Parameters.AddWithValue("@UserPwd", UserPwd);
35                     cmd.ExecuteNonQuery();
36                 }
37             }
38             context.Response.Redirect("index.ashx");
39         }
40         
41         public bool IsReusable
42         {
43             get
44             {
45                 return false;
46             }
47         }
48     }
49 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap