• 用MVC4练习,后台用aspx,数据库DemoDb《MvcUserDemo》


    将ado.net的cs文件SqlHelper.cs放入解决方案

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Configuration;
      6 using System.Data.SqlClient;
      7 using System.Data;
      8 
      9 namespace MvcUserDemo
     10 {
     11     public static class  SqlHelper
     12     {
     13         public static readonly string connstr =
     14             ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
     15 
     16         public static SqlConnection OpenConnection()
     17         {
     18             SqlConnection conn = new SqlConnection(connstr);
     19             conn.Open();
     20             return conn;
     21         }
     22 
     23         public static int ExecuteNonQuery(string cmdText,
     24             params SqlParameter[] parameters)
     25         {
     26             using (SqlConnection conn = new SqlConnection(connstr))
     27             {
     28                 conn.Open();
     29                 return ExecuteNonQuery(conn, cmdText, parameters);
     30             }
     31         }
     32 
     33         public static object ExecuteScalar(string cmdText,
     34             params SqlParameter[] parameters)
     35         {
     36             using (SqlConnection conn = new SqlConnection(connstr))
     37             {
     38                 conn.Open();
     39                 return ExecuteScalar(conn, cmdText, parameters);
     40             }
     41         }
     42 
     43         public static DataTable ExecuteDataTable(string cmdText,
     44             params SqlParameter[] parameters)
     45         {
     46             using (SqlConnection conn = new SqlConnection(connstr))
     47             {
     48                 conn.Open();
     49                 return ExecuteDataTable(conn, cmdText, parameters);
     50             }
     51         }
     52 
     53         public static int ExecuteNonQuery(SqlConnection conn,string cmdText,
     54            params SqlParameter[] parameters)
     55         {
     56             using (SqlCommand cmd = conn.CreateCommand())
     57             {
     58                 cmd.CommandText = cmdText;
     59                 cmd.Parameters.AddRange(parameters);
     60                 return cmd.ExecuteNonQuery();
     61             }
     62         }
     63 
     64         public static object ExecuteScalar(SqlConnection conn, string cmdText,
     65             params SqlParameter[] parameters)
     66         {
     67             using (SqlCommand cmd = conn.CreateCommand())
     68             {
     69                 cmd.CommandText = cmdText;
     70                 cmd.Parameters.AddRange(parameters);
     71                 return cmd.ExecuteScalar();
     72             }
     73         }
     74 
     75         public static DataTable ExecuteDataTable(SqlConnection conn, string cmdText,
     76             params SqlParameter[] parameters)
     77         {
     78             using (SqlCommand cmd = conn.CreateCommand())
     79             {
     80                 cmd.CommandText = cmdText;
     81                 cmd.Parameters.AddRange(parameters);
     82                 using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
     83                 {
     84                     DataTable dt = new DataTable();
     85                     adapter.Fill(dt);
     86                     return dt;
     87                 }
     88             }
     89         }
     90 
     91         public static object ToDBValue(this object value)
     92         {
     93             return value == null ? DBNull.Value : value;
     94         }
     95 
     96         public static object FromDBValue(this object dbValue)
     97         {
     98             return dbValue == DBNull.Value ? null : dbValue;
     99         }
    100     }
    101 }
    SqlHelper

    在web.config中配置连接数据库文件

    1 <connectionStrings>
    2     <add name="connstr" connectionString="server=.;uid=sa;pwd=321654;database=DemoDb"/>
    3   </connectionStrings>
    add

    创建一个控制器和页面,并获取数据库中的UserInfo表中的数据,把数据传递到前台页面进行展示

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Data;
     4 using System.Linq;
     5 using System.Web;
     6 using System.Web.Mvc;
     7 
     8 namespace MvcUserDemo.Controllers
     9 {
    10     public class UserInfoController : Controller
    11     {
    12         //
    13         // GET: /UserInfo/
    14 
    15         public ActionResult Index()
    16         {
    17             //获取数据库中的UserInfo表中的数据
    18             DataTable dt=SqlHelper.ExecuteDataTable("select Id, UserName, Age from dbo.UserInfo");
    19 
    20             //把数据传递到前台页面进行展示
    21             ViewData["dt"] = dt;
    22             return View();
    23         }
    24 
    25     }
    26 }
    UserInfoController.cs

     前台页面展示数据库中的数据

     1 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
     2 
     3 <%@ Import Namespace="System.Data" %>
     4 
     5 <!DOCTYPE html>
     6 
     7 <html>
     8 <head runat="server">
     9     <meta name="viewport" content="width=device-width" />
    10     <title>Index</title>
    11 </head>
    12 <body>
    13     <div>
    14         <%
    15             DataTable dt = (DataTable)ViewData["dt"]; 
    16           
    17         %>
    18 
    19         <table>
    20             <tr>
    21                 <th>编号</th>
    22                 <th>姓名</th>
    23                 <th>年龄</th>
    24             </tr>
    25 
    26             <%foreach (DataRow dataRow in dt.Rows)
    27               {%>
    28 
    29             <tr>
    30                 <td />
    31                 <%:dataRow["Id"] %><td />
    32                 <td />
    33                 <%:dataRow["UserName"]%><td />
    34                 <td><%:dataRow["Age"]%>
    35                 <td />
    36             </tr>
    37 
    38             <%}%>
    39         </table>
    40     </div>
    41 </body>
    42 </html>
    Index.aspx

     用户注册模块(拿到表单里面传递过来的数据,往数据库插入数据,返回首页)

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Data;
     4 using System.Linq;
     5 using System.Web;
     6 using System.Web.Mvc;
     7 using System.Data;
     8 using System.Data.SqlClient;
     9 
    10 namespace MvcUserDemo.Controllers
    11 {
    12     public class UserInfoController : Controller
    13     {
    14         #region 用户列表
    15 
    16         //
    17         // GET: /UserInfo/
    18 
    19         public ActionResult Index()
    20         {
    21             //获取数据库中的UserInfo表中的数据
    22             DataTable dt = SqlHelper.ExecuteDataTable("select Id, UserName, Age from dbo.UserInfo");
    23 
    24             //把数据传递到前台页面进行展示
    25             ViewData["dt"] = dt;
    26             return View();
    27         }
    28         #endregion
    29 
    30         #region 用户注册页面
    31         public ActionResult Add()
    32         {
    33             return View();
    34         }
    35 
    36         #endregion
    37         //用户注册方法    
    38         public ActionResult ProcessAdd(FormCollection collection)
    39         {
    40             // 拿到表单里面传递过来的数据
    41             string userName = Request["UserName"];
    42             int Age = Convert.ToInt32(Request["Age"]);
    43             //int Age = int.Parse(collection["Age"] ?? "0");
    44             //往数据库插入数据
    45             string insertSql = "insert UserInfo values(@UserName,@Age)";
    46             SqlHelper.ExecuteNonQuery(insertSql,
    47                 new SqlParameter("@UserName", userName),
    48                 new SqlParameter("@Age", Age));
    49 
    50             // return Content("OK");
    51             return RedirectToAction("Index");
    52         }
    53 
    54     }
    55 }
    ProcessAdd
     1 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
     2 
     3 <!DOCTYPE html>
     4 
     5 <html>
     6 <head runat="server">
     7     <meta name="viewport" content="width=device-width" />
     8     <title>用户注册</title>
     9 </head>
    10 <body>
    11     <div>
    12         <form method="post" action="/UserInfo/ProcessAdd">
    13             <table>
    14                 <tr>
    15                     <td>用户名:</td>
    16                     <td>
    17                         <input type="text" name="UserName"></td>
    18                 </tr>
    19                 <tr>
    20                     <td>年龄:</td>
    21                     <td>
    22                         <input type="text" name="Age"></td>
    23                 </tr>
    24                 <tr>
    25                     <td colspan="2">
    26                         <input type="submit" value="用户注册"></td>
    27                 </tr>
    28             </table>
    29         </form>
    30     </div>
    31 </body>
    32 </html>
    用户注册前台页面

     

    强类型视图

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 
     6 namespace MvcUserDemo.Models
     7 {
     8     public class UserInfo
     9     {
    10         public string UserName { get; set; }
    11         public int Id { get; set; }
    12         public int Age { get; set; }
    13     }
    14 }
    创建一个类

    (显示用户)显示models中的数据

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Data;
     4 using System.Linq;
     5 using System.Web;
     6 using System.Web.Mvc;
     7 using System.Data;
     8 using System.Data.SqlClient;
     9 using MvcUserDemo.Models;
    10 
    11 namespace MvcUserDemo.Controllers
    12 {
    13     public class UserInfoController : Controller
    14     {
    15         #region 用户列表
    16 
    17         //
    18         // GET: /UserInfo/
    19 
    20         public ActionResult Index()
    21         {
    22             //获取数据库中的UserInfo表中的数据
    23             DataTable dt = SqlHelper.ExecuteDataTable("select Id, UserName, Age from dbo.UserInfo");
    24 
    25             //把数据传递到前台页面进行展示
    26             ViewData["dt"] = dt;
    27             return View();
    28         }
    29         #endregion
    30 
    31         #region 用户注册页面
    32         public ActionResult Add()
    33         {
    34             return View();
    35         }
    36 
    37         #endregion
    38         //用户注册方法    
    39         public ActionResult ProcessAdd(FormCollection collection)
    40         {
    41             // 拿到表单里面传递过来的数据
    42             string userName = Request["UserName"];
    43             int Age = Convert.ToInt32(Request["Age"]);
    44             //int Age = int.Parse(collection["Age"] ?? "0");
    45             //往数据库插入数据
    46             string insertSql = "insert UserInfo values(@UserName,@Age)";
    47             SqlHelper.ExecuteNonQuery(insertSql,
    48                 new SqlParameter("@UserName", userName),
    49                 new SqlParameter("@Age", Age));
    50 
    51             // return Content("OK");
    52             return RedirectToAction("Index");
    53         }
    54 
    55 
    56         #region 显示用户
    57         public ActionResult Show()
    58         {
    59             UserInfo userInfo = new UserInfo();
    60             userInfo.Id = 9;
    61             userInfo.UserName = "你懂的";
    62             userInfo.Age = 18;
    63             ViewData.Model = userInfo;
    64             return View();
    65         }
    66         #endregion
    67     }
    68 }
    后台代码
     1 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcUserDemo.Models.UserInfo>" %>
     2 
     3 <%@ Import Namespace="MvcUserDemo.Models" %>
     4 <!DOCTYPE html>
     5 
     6 <html>
     7 <head runat="server">
     8     <meta name="viewport" content="width=device-width" />
     9     <title>Show</title>
    10 </head>
    11 <body>
    12     <div>
    13         <table>
    14             <tr>
    15                 <td>ID:</td>
    16                 <td><%:Model.Id %></td>
    17                 <td>姓名:</td>
    18                 <td><%:Model.UserName %></td>
    19                 <td>年龄:</td>
    20                 <td><%:Model.Age %></td>
    21             </tr>
    22         </table>
    23     </div>
    24     <%:Html.ActionLink("回到首页","Index") %>
    25 </body>
    26 </html>
    前台代码

     用户的删除

    修改Index页面,并引入jquery文件

    前台代码
     1 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
     2 
     3 <%@ Import Namespace="System.Data" %>
     4 
     5 <!DOCTYPE html>
     6 
     7 <html>
     8 <head runat="server">
     9     <meta name="viewport" content="width=device-width" />
    10     <title>Index</title>
    11     <script src="../../Scripts/jquery-1.8.2.min.js"></script>
    12     <script>
    13         $(function () {
    14             $("a:contains('删除')").click(function () {
    15                 return confirm("请问是否删除此数据?");
    16 
    17             });
    18         });
    19 
    20     </script>
    21 
    22 </head>
    23 <body>
    24     <div>
    25         <%
    26             DataTable dt = (DataTable)ViewData["dt"]; 
    27           
    28         %>
    29 
    30         <table>
    31             <tr>
    32                 <th>编号</th>
    33                 <th>姓名</th>
    34                 <th>年龄</th>
    35                 <th>删除</th>
    36             </tr>
    37 
    38             <%foreach (DataRow dataRow in dt.Rows)
    39               {%>
    40 
    41             <tr>
    42                 <td />
    43                 <%:dataRow["Id"] %><td />
    44                 <td />
    45                 <%:dataRow["UserName"]%><td />
    46                 <td><%:dataRow["Age"]%>
    47                 <td />
    48                 <td><%:Html.ActionLink("删除", "Delete", "UserInfo", new { Id = dataRow["Id"] }, new { })%>
    49                 <td />
    50             </tr>
    51 
    52             <%}%>
    53         </table>
    54     </div>
    55 </body>
    56 </html>

    在UserInfo中添加一个新的方法

     1  #region 删除
     2         public ActionResult Delete(int Id)
     3         {
     4             //根据Id删除用户的数据
     5             string sql = "delete from UserInfo where Id=@Id";
     6             SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@Id", Id));
     7 
     8             //页面跳转到删除后的首页
     9 
    10             return RedirectToAction("Index");
    11         }
    12         #endregion
    后台代码

    用户的修改

     1   #region 修改用户
     2 
     3         [HttpGet]
     4         public ActionResult Edit(int Id)
     5         {
     6             string sql = "select UserName,Id,Age from UserInfo where Id=@Id";
     7             DataTable dt = SqlHelper.ExecuteDataTable(sql, new SqlParameter("@Id", Id));
     8 
     9             //把dt转成UserInfo对象
    10             UserInfo userInfo = new UserInfo();
    11             userInfo.Id = Convert.ToInt32(dt.Rows[0]["Id"]);
    12             userInfo.Age = Convert.ToInt32(dt.Rows[0]["Age"]);
    13             userInfo.UserName = dt.Rows[0]["UserName"].ToString();
    14             ViewData.Model = userInfo;
    15             return View();
    16         }
    17 
    18         //只是显示用户修改的页面
    19         [HttpPost]
    20         public ActionResult Edit(int Id, int Age, string UserName, UserInfo userInfo)
    21         {
    22             string updateSql = "update UserInfo set UserName=@UserName,Age=@Age where Id=@Id";
    23             SqlParameter idParameter = new SqlParameter("@Id", userInfo.Id);
    24             SqlParameter ageParameter = new SqlParameter("@Age", userInfo.Age);
    25             SqlParameter nameParameter = new SqlParameter("@UserName", userInfo.UserName);
    26             SqlHelper.ExecuteNonQuery(updateSql, idParameter, ageParameter, nameParameter);
    27             return RedirectToAction("Index");
    28         }
    29         #endregion
    后台代码
     1 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcUserDemo.Models.UserInfo>" %>
     2 
     3 <!DOCTYPE html>
     4 
     5 <html>
     6 <head runat="server">
     7     <meta name="viewport" content="width=device-width" />
     8     <title>Edit</title>
     9     <script src="../../Scripts/jquery-1.8.2.min.js"></script>
    10     <script>
    11         $(function () {
    12             $("#xiugai").click(function () {
    13 
    14                 return confirm("请问是否修改此数据?");
    15             });
    16         
    17         });
    18            
    19         </script>
    20 </head>
    21 <body>
    22 
    23     <%using (Html.BeginForm())
    24       {%>
    25     <table>
    26         <tr>
    27             <td>用户编号:</td>
    28             <td><%:Model.Id %>
    29                 <%:Html.HiddenFor(u=>u.Id)%>
    30             </td>
    31         </tr>
    32         <tr>
    33             <td>用户名:</td>
    34             <td><%:Html.TextBoxFor(u=>u.UserName) %></td>
    35             <tr>
    36         <tr>
    37             <td>年龄:</td>
    38             <td><%:Html.TextBoxFor(u=>u.Age)%></td>
    39         </tr>
    40         <tr>
    41             <td colspan="2">
    42                 <input type="submit" value="修改" id="xiugai" /></td>
    43         </tr>
    44 
    45     </table>
    46     <%} %>
    47 </body>
    48 </html>
    前台代码
  • 相关阅读:
    20175126《Java程序设计》第七周学习总结
    20175126《Java程序设计》第六周学习总结
    软工实践个人总结
    第06组 Beta版本演示
    第06组 Beta冲刺(4/5)
    第06组 Beta冲刺(5/5)
    第06组 Beta冲刺(3/5)
    第06组 Beta冲刺(2/5)
    第6组 Beta冲刺(1/5)
    第06组 Alpha事后诸葛亮
  • 原文地址:https://www.cnblogs.com/qinsilandiao/p/4964505.html
Copyright © 2020-2023  润新知