第一、先创建一个名为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. mysql快速插入大数据

    说的是插入数据,这个倒像是载入数据. 上一篇,是按照插入数据来写的,就是insert into,当时插入一万条实在是太慢了,大概是286734毫秒. insert into table values, ...

  2. linux inode cheat sheet

    sector:扇区,硬盘存储的最小单位,大小为0.5KB(512字节) block:块文件存取的最小单位,1 block=8 sector,大小4KB inode:存储文件元信息.内容包括 * 文件的 ...

  3. FTPClient用法

      某些数据交换,我们需要通过ftp来完成. sun.net.ftp.FtpClient 可以帮助我们进行一些简单的ftp客户端功能:下载.上传文件. 但如遇到创建目录之类的就无能为力了,我们只好利用 ...

  4. .net core redis使用

    整理下.net core的redis使用  可以分布式 Nuget安装 StackExchange.Redis using System; using System.Collections.Gener ...

  5. ClamAV学习【7】——病毒库文件格式学习

    搜查到一份详细的ClamAV病毒文件格式资料(http://download.csdn.net/detail/betabin/4215909),英文版,国内这资料不多的感觉. 重点看了下有关PE的病毒 ...

  6. Java中的内部类(一)静态内部类

    Java中的静态内部类:在定义的内部类前加static修饰符,此时的内部类就是静态内部类.通过一个例子来了解静态内部类都有哪些特点. public class Outer { //定义一个实例变量和一 ...

  7. “全栈2019”Java第九十八章:局部内部类访问作用域成员详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  8. “全栈2019”Java第一章:安装JDK11(Mac)

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 文章原文链接 “全栈2019”Java第一章:安装JDK11(Mac) 下一章 “全栈2019”Java ...

  9. 关于IDENTITY_INSERT的用法介绍

    IDENTITY_INSERT用于对表中的标识列进行显式插入操作时的设置.格式如下: set identity_insert TABLE_NAME ON/OFF 如果需要对表中定义为IDENTITY属 ...

  10. SQL Server 根据树状结构表生成以/号分割的路由字符串

    很多情况下,我们有必要把树形结构进行数据梳理.比如,要方便的过滤出一个父节点下的所有子节点等等... 这个时候,我们可以生成一个路径表字符串,在应用时只需要对该字符串进行索引即可达成目的. 目标:按图 ...