zoukankan      html  css  js  c++  java
  • MVC操作SQL数据库增删改查

    控制器代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MvcApplication1.Models;
    namespace MvcApplication1.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Home/
    
            public ActionResult Update(string code, string name, bool sex, string nation, DateTime birthday)
            {
                //送到数据库中去
                new InfoBF().Update(code, name, sex, nation, birthday);
                //跳转回显示界面
                return RedirectToAction("Index");
            }
            public ActionResult Edit(string id)
            {
                //根据主键查出数据来
                Info data = new InfoBF().Select(id);
                //ViewBag把数据带到视图中去
                ViewBag.Data = data;
                return View();
            }
            public ActionResult Add()
            {
                return View();
            }
            public ActionResult Insert(string code, string name, bool sex, string nation, DateTime birthday)
            {
                //送到数据库中去 
                new InfoBF().Insert(code, name, sex, nation, birthday);
                //跳转到Index动作上去
                return RedirectToAction("Index");
            }
            public ActionResult Delete(string id)
            {
                try
                {
                    new InfoBF().Delete(id);
                    return RedirectToAction("Index");
                }
                catch
                {
                    return RedirectToAction("Index","Error");
                }
            }
            public ActionResult Index()
            {
                ViewBag.Data = new InfoBF().Select();
                return View();
            }
            public ActionResult Details(string id)
            {
                ////获取要查看详细人员的主键值
                //string id = Request["id"].ToString();
                //根据主键值来找人员的信息
                ViewBag.Data = new InfoBF().Select(id);
    
                return View();
            }
        }
    }

    增加视图代码:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>Add</title>
    </head>
    <body>
        <div>
            <h1>添加人员</h1>
            <div>
                <form id="f1" name="f1" action="/Home/Insert" method="post">
                代号:<input type="text" name="code" /><br/>
                姓名:<input type="text" name="name" /><br/>
                性别:<input type="text" name="sex" /><br/>
                民族:<input type="text" name="nation" /><br/>
                生日:<input type="text" name="birthday" /><br/>
                <input type="submit" value="添加" />
                </form>
            </div>
        </div>
    </body>
    </html>

    修改视图代码:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    <%@ Import Namespace="MvcApplication1.Models" %>
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>Edit</title>
    </head>
    <body>
        <div>
            <h1>修改人员</h1>
            <div>
                <form id="f1" name="f1" action="/Home/Update" method="post">
                 <%
                     Info data = ViewBag.Data as Info; 
                     if(data != null)
                     {
                      %>
                代号:<input readonly="readonly" type="text" name="code" value="<%=data.Code %>" /><br/>
                姓名:<input type="text" name="name" value="<%=data.Name %>" /><br/>
                性别:<input type="text" name="sex"  value="<%=data.Sex %>"/><br/>
                民族:<input type="text" name="nation" value="<%=data.Nation %>" /><br/>
                生日:<input type="text" name="birthday" value="<%=data.Birthday %>" /><br/>
                <input type="submit" value="更新" />
                    <%} %>
                </form>
            </div>
        </div>
    </body>
    </html>

    主页目录视图代码:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    <%@ Import Namespace="MvcApplication1.Models" %>
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            <ul>
                <%
                    List<Info> list = ViewBag.Data as List<Info>;
                    foreach(Info data in list)
                    {
                     %>
                <li>
                        <a href="Home/Details/<%=data.Code %>"> <%=data.Name %></a>
                        <a href="Home/Edit/<%=data.Code %>">修改</a>
                        <a href="Home/Delete/<%=data.Code %>">删除</a>
                </li>
                <%
                    }
                     %>
            </ul>
            <a href="Home/Add">添加</a>
        </div>
    </body>
    </html>

    Model层增删改查Linq方法代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MvcApplication1.Models
    {
        
        public class InfoBF
        {
            private MyDBDataContext _Context = new MyDBDataContext();
            public List<Info> Select()
            {
                return _Context.Info.ToList();
            }
            public Info Select(string code)
            {
                var query = _Context.Info.Where(p=>p.Code == code);
                if (query.Count() > 0)
                {
                    return query.First();
                }
                return null;
            }
            public void Update(string code, string name, bool sex, string nation, DateTime birthday)
            {
                 var query = _Context.Info.Where(p=>p.Code == code);
                 if (query.Count() > 0)
                 {
                     Info data = query.First();
                     data.Name = name;
                     data.Sex = sex;
                     data.Nation = nation;
                     data.Birthday = birthday;
                 }
                 _Context.SubmitChanges();
            }
            public void Insert(string code, string name, bool sex, string nation, DateTime birthday)
            {
                Info data = new Info
                {
                    Code = code,
                    Name = name,
                    Sex = sex,
                    Nation = nation,
                    Birthday = birthday
                };
                _Context.Info.InsertOnSubmit(data);
                _Context.SubmitChanges();
    
            }
            public void Delete(string code)
            {
                var query = _Context.Info.Where(p => p.Code == code);
                if (query.Count() > 0)
                {
                    Info data = query.First();
                    _Context.Info.DeleteOnSubmit(data);
                    _Context.SubmitChanges();
                }
            }
        }
    }
  • 相关阅读:
    HDOJ1269 迷宫城堡
    最长公共子序列 nyoj36
    HDU1081 To The Max 求子矩阵最大和
    nyoj20 吝啬的国度
    景观分析工具:arcgis中patch analysis模块
    景观格局动态变化分析方法(基于ArcGIS、Fragstats、ENVI、ERDAS、Patch Analysis for ArcGIS) (20110315 08:07:03)
    从C#到Python —— 谈谈我学习Python一周来的体会
    如何判定多边形是顺时针还是逆时针
    超新星与暗能量的发现--今年诺贝尔物理奖工作的介绍(转)
    怎样把扫描好的身份证打印出实际大小
  • 原文地址:https://www.cnblogs.com/dlexia/p/4649141.html
Copyright © 2011-2022 走看看