欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

c#中Linq to Sql 增删除的实例

时间:2020-12-29 16:46:21|栏目:.NET代码|点击:

抽像类:  

复制代码 代码如下:

 public abstract class AbUserAll
    {
        public abstract IQueryable<User_ALL> FindUserAll();
        public abstract User_ALL FindUserAllById(int userid);
        public abstract void Add(User_ALL user);
        public abstract void Update(User_ALL user);
        public abstract void Delete(User_ALL user);
        public abstract void Save();
    }

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WF.Models.DAL
{
    public class UserDal:IDAL.AbUserAll
    {
        private PL_ModelDataContext db = new PL_ModelDataContext();
        public override IQueryable<User_ALL> FindUserAll()
        {
            return db.User_ALL;
        }

        public override void Delete(User_ALL user)
        {
            db.User_ALL.DeleteOnSubmit(user);
        }

        public override void Save()
        {
            db.SubmitChanges();
        }

        public override User_ALL FindUserAllById(int userid)
        {
            return db.User_ALL.SingleOrDefault(o => o.INT == userid);
        }

        public override void Add(User_ALL user)
        {
            db.User_ALL.InsertOnSubmit(user);
        }

        public override void Update(User_ALL user)
        {
            var editStudent = db.User_ALL.SingleOrDefault<User_ALL>(s => s.INT == user.INT);
            if (editStudent != null)
            {
                editStudent.UserName = user.UserName;
                editStudent.Address = user.Address;
            }
            db.SubmitChanges();

        }
    }
}

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WF.Controllers
{
    public class UserController : Controller
    {
        Models.DAL.UserDal dbuser = new Models.DAL.UserDal();
        //
        // GET: /User/

        public ActionResult UserList()
        {

            return View(dbuser.FindUserAll());
        }

 
        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection,Models.User_ALL user)
        {
            try
            {
                dbuser.Add(user);
                dbuser.Save();
                return RedirectToAction("UserList");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Edit/5

        public ActionResult UserEdit(int id)
        {

            return View(dbuser.FindUserAllById(id));
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult UserEdit(Models.User_ALL user, FormCollection collection)
        {
            try
            { 
                dbuser.Update(user);
                dbuser.Save();
                return RedirectToAction("UserList");
            }
            catch
            {
                return View();
            }
        }
    }
}

上一篇:C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法

栏    目:.NET代码

下一篇:asp.net 2.0里也可以用JSON的使用方法

本文标题:c#中Linq to Sql 增删除的实例

本文地址:http://www.codeinn.net/misctech/37994.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有