MVC 中使用EF
EF
1)简单查询
后台代码
using MvcApplication18.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcApplication18.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
appDBEntities db = new appDBEntities();
public ActionResult Index()
{
SqlParameter[] parameter = { };
List<SecondHand_info> list = db.Database.SqlQuery<SecondHand_info>("select * from SecondHand_info where SecondHand_keyID >88", parameter).ToList();
ViewData["list"] = list;
return View();
} }
}
前台代码
@model MvcApplication18.Models.SecondHand_info
@using MvcApplication18.Models
@{
ViewBag.Title = "Index";
} @foreach (SecondHand_info item in ViewData["list"] as List<SecondHand_info>)
{
<p>@item.SecondHand_keyID </p>
<p>@item.SecondHand_mobile </p>
<p>@item.SecondHand_title </p>
<p>@item.SecondHand_context </p> }
2)带参数查询
后台代码
using MvcApplication18.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcApplication18.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
appDBEntities db = new appDBEntities();
public ActionResult Index()
{
var id = "";
var mobile = "";
var sql = "select * from SecondHand_info where SecondHand_keyID =@id and SecondHand_mobile=@mobile";
List<SecondHand_info> list= db.Database.SqlQuery<SecondHand_info>(
sql,
new SqlParameter("@id",id),
new SqlParameter("@mobile",mobile) ).ToList();
db.SaveChanges();
ViewData["list"] = list;
return View();
} }
}
前台代码
@model MvcApplication18.Models.SecondHand_info
@using MvcApplication18.Models
@{
ViewBag.Title = "Index";
} @foreach (SecondHand_info item in ViewData["list"] as List<SecondHand_info>)
{
<p>@item.SecondHand_keyID </p>
<p>@item.SecondHand_mobile </p>
<p>@item.SecondHand_title </p>
<p>@item.SecondHand_context </p> }
<h2>Index</h2>
3)修改
后台代码
using MvcApplication18.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcApplication18.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
appDBEntities db = new appDBEntities();
public ActionResult Index()
{ //修改
List<SecondHand_info> list = db.Set<SecondHand_info>().SqlQuery("select * from SecondHand_info where SecondHand_keyID =88").ToList();
list.Last().SecondHand_mobile = "";//把SecondHand_keyID =88 的手机号改为18017848011
db.SaveChanges();
ViewData["list"] = list;
return View();
} }
}
前台代码
@model MvcApplication18.Models.SecondHand_info
@using MvcApplication18.Models
@{
ViewBag.Title = "Index";
} @foreach (SecondHand_info item in ViewData["list"] as List<SecondHand_info>)
{
<p>@item.SecondHand_keyID </p>
<p>@item.SecondHand_mobile </p>
<p>@item.SecondHand_title </p>
<p>@item.SecondHand_context </p> }
4)带参数的存储过程(所以字段都要列出不然会出现问题)
create proc P_QuerybyId_secondhand
@SecondHand_keyID int
as
begin
select SecondHand_keyID, SecondHand_mobile, SecondHand_title, SecondHand_new, SecondHand_type, SecondHand_money, SecondHand_context, SecondHand_pic1, SecondHand_pic2, SecondHand_pic3, SecondHand_pic4, SecondHand_pic5, SecondHand_pic, SecondHand_time, Memo FROM SecondHand_info where SecondHand_keyID=@SecondHand_keyID
end
using MvcApplication18.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcApplication18.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
appDBEntities db = new appDBEntities();
public ActionResult Index()
{ var param = new SqlParameter("SecondHand_keyID", );
List<SecondHand_info> list = db.Database.SqlQuery<SecondHand_info>("P_QuerybyId_secondhand @SecondHand_keyID", param).ToList();
db.SaveChanges();
ViewData["list"] = list;
return View();
} }
}
@model MvcApplication18.Models.SecondHand_info
@using MvcApplication18.Models
@{
ViewBag.Title = "Index";
} @foreach (SecondHand_info item in ViewData["list"] as List<SecondHand_info>)
{
<p>@item.SecondHand_keyID </p>
<p>@item.SecondHand_mobile </p>
<p>@item.SecondHand_title </p>
<p>@item.SecondHand_context </p> }
<h2>Index</h2>
5)带输出参数的存储过程
CREATE PROCEDURE [dbo].[Myproc]
@Name NVARCHAR(max),
@PageIndex int,
@PageSize INT,
@TotalCount int OUTPUT
as declare @startRow int
declare @endRow int set @startRow = (@PageIndex - 1) * @PageSize + 1
set @endRow = @startRow + @PageSize - 1 select *
FROM
(
select top (@endRow)
ID,
Age,
Name,
row_number() over(order by [ID] desc) as [RowIndex]
from dbo.Student
) as T
where [RowIndex] >= @startRow AND T.Name = @Name SET @TotalCount=(select count(1) as N
FROM dbo.Student WHERE Name = @Name)
var name = new SqlParameter { ParameterName = "Name", Value = Name };
var currentpage = new SqlParameter { ParameterName = "PageIndex", Value = currentPage };
var pagesize = new SqlParameter { ParameterName = "PageSize", Value = pageSize };
var totalcount = new SqlParameter { ParameterName = "TotalCount", Value = , Direction = ParameterDirection.Output }; var list = ctx.Database.SqlQuery<Student>("Myproc @Name, @PageIndex, @PageSize, @TotalCount output",
name, currentpage, pagesize, totalcount); totalCount = (int)totalcount.Value; /*获得要输出参数totalcount的值*/
MVC 中使用EF的更多相关文章
- MVC中使用EF(2):实现基本的CRUD功能
MVC中使用EF(2):实现基本的CRUD功能 By Tom Dykstra |July 30, 2013 Translated by litdwg Contoso University示例网站 ...
- MVC中使用EF(1):为ASP.NET MVC程序创建Entity Framework数据模型
为ASP.NET MVC程序创建Entity Framework数据模型 (1 of 10) By Tom Dykstra |July 30, 2013 Translated by litdwg ...
- .NET Core2.0 MVC中使用EF访问数据
使用环境:Win7+VS2017 一.新建一个.NET Core2.0的MVC项目 二.使用Nuget添加EF的依赖 输入命令:Install-Package Microsoft.EntityFram ...
- MVC中使用EF增删改查,简单的例子
//这个是分页数据和总页数类 public class SummaryBase<TModel> { public SummaryBase(); public IList<TModel ...
- MVC中使用EF+MySQL
最近一段时间,想在EF中使用MySQL,于是就进行了测试. 我的环境是VS2013+EF6+MySQL5.7.4 首先要下载MySQL For VisualStudio 1.1.4.Connector ...
- MVC中使用EF的技巧集
1.从数据库生成模型后,再次更新模型时,之前设置的验证规则会丢失. 解决方法:在Models文件夹中新建一个空白类,把它命名为shujuyanzh.cs(类名可以自定),然后把Models中自动生成的 ...
- MVC中使用EF:排序,过滤,分页
原文链接:http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging ...
- MVC中使用EF的技巧集(一)
一.建好数据库后,向项目中添加数据模型. 1.右键点击“Models” 文件夹,选择“添加”,再选择“添加新项”. 2.在“添加新项”窗口,选择左边的“数据”,然后再在右边选择“ADO.NET 实体数 ...
- MVC中使用EF的技巧集(二)——分部验证
1.从数据库生成模型后,再次更新模型时,之前设置的验证规则会丢失. 解决方法:在Models文件夹中新建一个空白类,把它命名为shujuyanzh.cs(类名可以自定),然后把Models中自动生成的 ...
随机推荐
- 从一个小项目看return 引用 重载运算符
#ifndef _ARRAY_H_ #define _ARRAY_H_ class Array { private: int mLength; int* mSpace; public: Array(i ...
- Java实现文件的读写,复制
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStr ...
- mydetails-yii1
1.yii验证码多余的get a new code ,即使在main.php中配置了中文也是出现获取新图片,影响效果 需要把 <?php $this->widget('CCaptcha') ...
- Three.js - 网页3D应用的普及已经指日可待了
Three.js可以高效实现3D效果,真不赖.
- COM编程之三 QueryInterface
[1]IUnknown接口 客户同组件交互都是通过接口完成的. 在客户查询组件的其它接口时,也是通过接口完成的.而那个接口就是IUnknown. IUnknown接口的定义包含在Win32SDK中的U ...
- 161018、springMVC中普通类获取注解service方法
1.新建一个类SpringBeanFactoryUtils 实现 ApplicationContextAware package com.loiot.baqi.utils; import org.sp ...
- Linux I2C工具查看配置I2C设备【转】
转自:http://blog.chinaunix.net/uid-26895763-id-3478882.html 在處理音訊相關的問題時,我通常會找個方法來讀寫codec中register的值.幸好 ...
- Linux Runtime PM介绍【转】
转自:http://blog.csdn.net/wlwl0071986/article/details/42677403 一.Runtime PM引言 1. 背景 (1)display的需求 (2)系 ...
- 使用BBCP来提升跨互联网的数据传输速度
背景介绍: 目前项目在美国东西部以及欧洲都有服务器节点,跨互联网的数据传输速度很不稳定,之前我们主要是通过SCP以及Rsync等方式进行数据传输的. 无意间发现了BBCP这个软件之后,经过测试,效果非 ...
- iOS-网址集
0. 在线工具 http://tool.lu 1. iOS学习笔记汇总链接 https://blog.6ag.cn/533.html 2.iOS开发内购全套图文教程http://mp.weixin.q ...