c#中Linq to Sql 增删除的实例
程序员文章站
2023-12-18 16:53:40
抽像类: 复制代码 代码如下: public abstract class abuserall {&n...
抽像类:
复制代码 代码如下:
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();
}
}
}
}