当前位置:Gxlcms > 数据库问题 > MVC操作SQL数据库增删改查

MVC操作SQL数据库增删改查

时间:2021-07-01 10:21:17 帮助过:3人阅读

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();
            }
        }
    }
}

 

MVC操作SQL数据库增删改查

标签:

人气教程排行