第一、先创建一个名为Store数据库,将下面脚本代码执行创建表;

USE [Store]
GO /****** Object: Table [dbo].[Category] Script Date: 03/25/2014 09:39:23 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE TABLE [dbo].[Category](
[CategoryID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED
(
[CategoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO /****** Object: Table [dbo].[Product] Script Date: 03/25/2014 09:39:31 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE TABLE [dbo].[Product](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[CategoryID] [int] NULL,
[UnitPrice] [decimal](18, 2) NULL,
[UnitsInStock] [int] NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO 第二、控制器ProductController
//
// GET: /Product/ public ActionResult Index()
{
var products = db.Products;
return View(products.ToList());
} /// <summary>
// GET: /Products/Details/5
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Details(int id = )
{
var product = db.Products.First(p => p.ID == id);
if (product == null)
return HttpNotFound();
return View(product);
} //
// GET: /Products/Create
public ActionResult Create()
{
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name");
return View();
} //
// POST: /Products/Create [HttpPost]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.AddToProducts(product);
db.SaveChanges();
return RedirectToAction("Index");
} ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name", product.CategoryID);
return View(product);
} //
//GET: /Products/Edit/5
public ActionResult Edit(int id = )
{
Product entity = db.Products.First(p => p.ID == id);
if (entity == null)
return HttpNotFound();
ViewBag.CategoryID = new SelectList(db.Categories.ToList(), "CategoryID", "Name", entity.CategoryID);
return View(entity);
} //
//POST: /Products/Edit/5
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
db.CreateObjectSet<Product>().Attach(product);
db.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryID = new SelectList(db.Categories.ToList(), "CategoryID", "Name", product.CategoryID);
return View(product);
} //
// GET: /Products/Delete/5 public ActionResult Delete(int id = )
{
Product product = db.Products.First(p => p.ID == id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
} //
// POST: /Products/Delete/5 [HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Product product = db.Products.First(p => p.ID == id);
db.CreateObjectSet<Product>().Attach(product);
db.ObjectStateManager.ChangeObjectState(product, EntityState.Deleted);
db.SaveChanges();
return RedirectToAction("Index");
} 第3、视图
视图结构:
下面是视图代码:
Index:
@model IEnumerable<TMVC.DAL.Product>
@{
ViewBag.Title = "Index";
}
<h2>
产品页面</h2>
<p>@Html.ActionLink("添加", "Create")</p>
<table>
<tr>
@*<th>@Html.DisplayNameFor(model => model.Name)
</th>
<th>@Html.DisplayNameFor(model => model.CategoryID)
</th>
<th>@Html.DisplayNameFor(model => model.UnitPrice)
</th>
<th>@Html.DisplayNameFor(model => model.UnitsInStock)
</th>*@
<th>
名称
</th>
<th>
类别
</th>
<th>
价格
</th>
<th>
库存
</th>
<th>
编辑
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(mi => item.Name)
</td>
<td>@Html.DisplayFor(mi => item.CategoryID)
</td>
<td>@Html.DisplayFor(mi => item.UnitPrice)
</td>
<td>@Html.DisplayFor(mi => item.UnitsInStock)
</td>
<td>@Html.ActionLink("编辑", "Edit", new { id = item.ID }) |
@Html.ActionLink("详细", "Details", new { id = item.ID }) | @Html.ActionLink("删除", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>

Create:

@model TMVC.DAL.Product
@{
ViewBag.Title = "Create";
}
<h2>
添加</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true);
<fieldset>
<legend>产品</legend>
<div class="editor-label">
名称 @* @Html.LabelFor(model => model.Name)*@
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
类别 @* @Html.LabelFor(model => model.CategoryID, "CategoryPCategor")*@
</div>
<div class="editor-field">
@*@Html.DropDownList("CategoryID", String.Empty)*@
@Html.EditorFor(model => model.CategoryID)
@Html.ValidationMessageFor(model => model.CategoryID)
</div>
<div class="editor-label">
价格 @*@Html.LabelFor(model => model.UnitPrice)*@
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitPrice)
@Html.ValidationMessageFor(model => model.UnitPrice)
</div>
<div class="editor-label">
库存@* @Html.LabelFor(model => model.UnitsInStock)*@
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitsInStock)
@Html.ValidationMessageFor(model => model.UnitsInStock)
</div>
<p>
<input type="submit" value="确定添加" />
</p>
</fieldset>
}

Delete:

@model TMVC.DAL.Product
@{
ViewBag.Title = "Delete";
}
<h2>
删除</h2>
<h3>
你确定要删除这个产品吗?</h3>
<fieldset>
<legend>产品</legend>
<div class="display-label">
名称
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
<div class="display-label">
类别
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
<div class="display-label">
价格
</div>
<div class="display-field">
@Html.DisplayFor(model => model.UnitPrice)
</div>
<div class="display-label">
库存
</div>
<div class="display-field">
@Html.DisplayFor(model => model.UnitsInStock)
</div>
</fieldset>
@using (Html.BeginForm())
{
<p>
<input type="submit" value="删除" />
|
@Html.ActionLink("返回产品列表", "Index")
</p>
}

Details:

@model TMVC.DAL.Product
@{
ViewBag.Title = "Details";
}
<h2>
详细</h2>
<fieldset>
<legend>产品</legend>
<p>
名称:@Html.DisplayFor(model => model.Name)</p>
<p>
类别:@Html.DisplayFor(model => model.CategoryID)</p>
<p>
价格:@Html.DisplayFor(model => model.UnitPrice)</p>
<p>
库存:@Html.DisplayFor(model => model.UnitsInStock)</p>
</fieldset>
<p>@Html.ActionLink("编辑", "Edit", new { id = Model.ID }) | @Html.ActionLink("返回", "Index")</p>

Edit:

@model TMVC.DAL.Product
@{
ViewBag.Title = "Edit";
}
<h2>
编辑</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true) <fieldset>
<legend>产品</legend>
@Html.HiddenFor(model => model.ID)
<div class="editor-label">
名称
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
类别
</div>
<div class="editor-field">
@*@Html.DropDownList("CategoryID", String.Empty)*@
@Html.EditorFor(model => model.CategoryID)
@Html.ValidationMessageFor(model => model.CategoryID)
</div>
<div class="editor-label">
价格
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitPrice)
@Html.ValidationMessageFor(model => model.UnitPrice)
</div>
<div class="editor-label">
库存
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitsInStock)
@Html.ValidationMessageFor(model => model.UnitsInStock)
</div>
<p>
<input type="submit" value="保存" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("返回产品列表", "Index")
</div>
RouteConfig
            routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

 

 

EF4.4增删改查实例的更多相关文章

  1. python链接oracle数据库以及数据库的增删改查实例

    初次使用python链接oracle,所以想记录下我遇到的问题,便于向我这样初次尝试的朋友能够快速的配置好环境进入开发环节. 1.首先,python链接oracle数据库需要配置好环境. 我的相关环境 ...

  2. java:JSP(JSPWeb.xml的配置,动态和静态导入JSP文件,重定项和请求转发,使用JSP实现数据库的增删改查实例)

    1.JSP的配置: <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  3. yii2.0增删改查实例讲解

    yii2.0增删改查实例讲解一.创建数据库文件. 创建表 CREATE TABLE `resource` ( `id` int(10) NOT NULL AUTO_INCREMENT, `textur ...

  4. 百度鹰眼Java接口调用增删改查实例

    因感觉百度鹰眼的使用场景比较符合实际业务,于是对百度鹰眼做了简单功能调试.刚开始使用springframework封装的RestTemplate,但是测试提示ak参数不存在.后又试了几种方法,均提示a ...

  5. 【php增删改查实例】第四节 -自己 DIY 一个数据库管理工具

    本节介绍如何自己DIY一个数据库管理工具,可以在页面输入sql 进行简单的增删改查操作. 首先,找到xampp的安装目录,打开htdocs: 新建一个php文件,名称为 mysqladmin.php ...

  6. Maven多模块项目+MVC框架+AJAX技术+layui分页对数据库增删改查实例

    昨天刚入门Maven多模块项目,所以简单写了一个小测试,就是对数据库单表的增删改查,例子比较综合,写得哪里不妥还望大神赐教,感谢! 首先看一下项目结构: 可以看到,一个项目MavenEmployee里 ...

  7. 关于利用PHP访问MySql数据库的逻辑操作以及增删改查实例操作

    PHP访问MySql数据库 <?php //造连接对象$db = new MySQLi("localhost","root","",& ...

  8. 【php增删改查实例】第十节 - 部门管理模块(新增功能)

    正常情况下,在一个部门管理页面,不仅仅需要展示列表数据,还需要基本的增删改操作,所以,我们先把之前写好的新增功能集成进来. 在toolbar中,添加一个新增按钮. <div id="t ...

  9. Java MVC 增删改查 实例

    需求:实现增加新部门的功能,对应数据库表示Oracle的dept表 一.Java MVC 增 实现: 1.视图层(V):注册部门 deptAdd.jsp 在注册新部门页面只需输入“部门名称”和“城市” ...

随机推荐

  1. 国际时区 TimeZone ID列表

    public static void main(String[] args) { Calendar c = new GregorianCalendar(); c.setTime(new Date()) ...

  2. java写入换行符

    写入一个文件,生成文本文档,里面写入1000行字符,但是写出来的没有换行.所以纠结,百度了下,一行完事. String crlf=System.getProperty("line.separ ...

  3. Zero Clipboard js+swf实现的复制功能使用方法

    开发中经常会用到复制的功能,在 IE 下实现比较简单.但要想做到跨浏览器比较困难了.本文将介绍一个跨浏览器的库类 Zero Clipboard .它利用 Flash 进行复制,所以只要浏览器装有 Fl ...

  4. PYQT4 Python GUI 编写与 打包.exe程序

    工作中需要开发一个小工具,简单的UI界面可以很好的提高工具的实用性,由此开启了我的第一次GUI开发之旅,下面将自己学习的心得记录一下,也做为学习笔记吧!!! 参考:http://www.qaulau. ...

  5. 3:C#异步WaitAll的使用

    编写界面如图: private async void button1_Click(object sender, EventArgs e) { #region 单个执行的异步,效率慢 HttpClien ...

  6. 开源应用框架BitAdminCore:更新日志20180817

    索引 NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/coo ...

  7. Spring Boot 学习系列(06)—采用log4j2记录日志

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 为什么选择log4j2 log4j2相比于log4j1.x和logback来说,具有更快的执行速度.同时也支 ...

  8. Vulnhub Breach1.0

    1.靶机信息 下载链接 https://download.vulnhub.com/breach/Breach-1.0.zip 靶机说明 Breach1.0是一个难度为初级到中级的BooT2Root/C ...

  9. Java反射与自定义注解

    反射,在Java常用框架中屡见不鲜.它存在于java.lang.reflact包中,就我的认识,它可以拿到类的字段和方法,及构造方法,还可以生成对象实例等.对深入的机制我暂时还不了解,本篇文章着重在使 ...

  10. 读取Properties文件的六种方法

    1.使用java.util.Properties类的load()方法 示例: InputStream in = new BufferedInputStream(new FileInputStream( ...