手机
当前位置:查字典教程网 >编程开发 >C#教程 >c#中Linq to Sql 增删除的实例
c#中Linq to Sql 增删除的实例
摘要:抽像类:复制代码代码如下:publicabstractclassAbUserAll{publicabstractIQueryableFind...

抽像类:

复制代码 代码如下:

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#中Linq to Sql 增删除的实例】相关文章:

C#中通过API实现的打印类 实例代码

c#启动EXE文件的方法实例

c# SendMail发送邮件实例代码

C# 屏蔽关键字的实现方法

c# 开机启动项的小例子

c#典型工厂化实现实例

C# String Replace高效的实例方法

深入C#中使用SqlDbType.Xml类型参数的使用详解

c# winform多线程的小例子

C#控制台输出进度和百分比的实例代码

精品推荐
分类导航