码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-07-04 13:25:36      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:namespace   direct   ==   sel   read   ram   tar   sql   xmlns   

说明(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 }

 

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

标签:namespace   direct   ==   sel   read   ram   tar   sql   xmlns   

原文地址:http://www.cnblogs.com/Jacklovely/p/7115909.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!