MVC实现增删查改

1.添加Northwind.edmx(ADO.NET Entity Data Model).

2. 在HomeController中添加如下代码:

    [HandleError]
    public class HomeController : Controller
    {
        NorthwindEntities NorthWind = new NorthwindEntities();
        public ActionResult Index()
        {
            var model = NorthWind.Categories.ToList();
            return View(model);
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int id)
        {
            // 返回集合中的第一个元素,其实质就是在SQL语句中加TOP (1)。
            var model = NorthWind.Categories.First(c => c.CategoryID == id);
            return View(model);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id,FormCollection form)
        {
            var model = NorthWind.Categories.First(c => c.CategoryID == id);
            UpdateModel(model,new []{"CategoryName","Description"});
            NorthWind.SaveChanges();
            return RedirectToAction("Index");
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Details(int id)
        {
            var model = NorthWind.Categories.First(c => c.CategoryID == id);
            return View(model);
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            Categories categories=new Categories();
            return View(categories);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(int CategoryID,FormCollection form)
        {
            //当序列中可能找不到满足条件的元素时,使用FirstOrDefault方法,然后,一定要对返回值是否为null,进行不同的处理
            var model = NorthWind.Categories.FirstOrDefault(c => c.CategoryID == CategoryID);
            if (model==null)
            {
                Categories categories=new Categories();
                UpdateModel(categories,new[]{"CategoryName","Description"});
                NorthWind.AddToCategories(categories);
                NorthWind.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Create");
            }
        }
        public ActionResult About()
        {
            return View();
        }
    }


3.在Index处右键,然后Add View,如下图:

MVC

 

4. 用以上方法依次添加增删查改

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MVC模式的实现对数据库的增删改查 部分代码: package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import common.DBConnection; import bean.Contact; public class ContactDAO { public List getAllContact() throws Exception{ Connection conn=DBConnection.getConntion(); PreparedStatement ps=conn.prepareStatement("select * from Contact"); ResultSet rs=ps.executeQuery(); List list = new ArrayList(); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String phone = rs.getString("phone"); String address = rs.getString("address"); Contact c = new Contact(); c.setId(id); c.setName(name); c.setPhone(phone); c.setAddress(address); list.add(c); } rs.close(); ps.close(); conn.close(); return list; } public void addContact(String name,String phone,String address) throws Exception{ String sql = "insert into contact(id,name,phone,address) values(seq_contact.nextval,?,?,?)"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, phone); pstmt.setString(3, address); pstmt.executeUpdate(); } public void delContact(int id) throws Exception{ String sql = "delete from contact where id=?"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, id); pstmt.executeUpdate(); } public Contact getContactById(int id) throws Exception{ String sql = "select * from Contact where id=?"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, id); ResultSet rs = pstmt.executeQuery(); Contact c = null; while(rs.next()){ // int id = rs.getInt("id"); String name=rs.getString("name"); String p

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值