MVC 中使用EF

EF

1)简单查询

后台代码

 1 using MvcApplication18.Models;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Data.SqlClient;
 5 using System.Linq;
 6 using System.Web;
 7 using System.Web.Mvc;
 8 
 9 namespace MvcApplication18.Controllers
10 {
11     public class HomeController : Controller
12     {
13         //
14         // GET: /Home/
15         appDBEntities db = new appDBEntities();
16         public ActionResult Index()
17         {
18             SqlParameter[] parameter = { };
19             List<SecondHand_info> list = db.Database.SqlQuery<SecondHand_info>("select * from SecondHand_info where SecondHand_keyID >88", parameter).ToList();
20           ViewData["list"] = list;
21             return View();
22         }
23 
24     }
25 }
View Code

前台代码

 1 @model MvcApplication18.Models.SecondHand_info
 2 @using MvcApplication18.Models
 3 @{
 4     ViewBag.Title = "Index";
 5 }
 6 
 7 @foreach (SecondHand_info item in ViewData["list"] as List<SecondHand_info>)
 8 {
 9   <p>@item.SecondHand_keyID </p> 
10      <p>@item.SecondHand_mobile </p> 
11      <p>@item.SecondHand_title </p> 
12      <p>@item.SecondHand_context </p> 
13   
14 }
View Code

 2)带参数查询

后台代码

 1 using MvcApplication18.Models;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Data.SqlClient;
 5 using System.Linq;
 6 using System.Web;
 7 using System.Web.Mvc;
 8 
 9 namespace MvcApplication18.Controllers
10 {
11     public class HomeController : Controller
12     {
13         //
14         // GET: /Home/
15         appDBEntities db = new appDBEntities();
16         public ActionResult Index()
17         {
18             var id = "88";
19             var mobile = "18017848011";
20             var sql = "select * from SecondHand_info where SecondHand_keyID =@id and SecondHand_mobile=@mobile";
21          List<SecondHand_info> list=   db.Database.SqlQuery<SecondHand_info>(
22                 sql,
23                 new SqlParameter("@id",id),
24                 new SqlParameter("@mobile",mobile)
25 
26                 ).ToList();
27          db.SaveChanges();
28           ViewData["list"] = list;
29             return View();
30         }
31 
32     }
33 }
View Code

前台代码

 1 @model MvcApplication18.Models.SecondHand_info
 2 @using MvcApplication18.Models
 3 @{
 4     ViewBag.Title = "Index";
 5 }
 6 
 7 @foreach (SecondHand_info item in ViewData["list"] as List<SecondHand_info>)
 8 {
 9   <p>@item.SecondHand_keyID </p> 
10      <p>@item.SecondHand_mobile </p> 
11      <p>@item.SecondHand_title </p> 
12      <p>@item.SecondHand_context </p> 
13   
14 }
15 <h2>Index</h2>
View Code

 

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 = "18017848011";//把SecondHand_keyID =88 的手机号改为18017848011
            db.SaveChanges();
          ViewData["list"] = list;
            return View();
        }

    }
}
View Code

前台代码

 1 @model MvcApplication18.Models.SecondHand_info
 2 @using MvcApplication18.Models
 3 @{
 4     ViewBag.Title = "Index";
 5 }
 6 
 7 @foreach (SecondHand_info item in ViewData["list"] as List<SecondHand_info>)
 8 {
 9   <p>@item.SecondHand_keyID </p> 
10      <p>@item.SecondHand_mobile </p> 
11      <p>@item.SecondHand_title </p> 
12      <p>@item.SecondHand_context </p> 
13   
14 }
View Code

 4)带参数的存储过程(所以字段都要列出不然会出现问题

1 create proc P_QuerybyId_secondhand
2 @SecondHand_keyID int
3 as
4 begin
5 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
6 end
View Code
 1 using MvcApplication18.Models;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Data.SqlClient;
 5 using System.Linq;
 6 using System.Web;
 7 using System.Web.Mvc;
 8 
 9 namespace MvcApplication18.Controllers
10 {
11     public class HomeController : Controller
12     {
13         //
14         // GET: /Home/
15         appDBEntities db = new appDBEntities();
16         public ActionResult Index()
17         {
18          
19             var param = new SqlParameter("SecondHand_keyID", 89);
20             List<SecondHand_info> list = db.Database.SqlQuery<SecondHand_info>("P_QuerybyId_secondhand @SecondHand_keyID", param).ToList();
21          db.SaveChanges();
22           ViewData["list"] = list;
23             return View();
24         }
25 
26     }
27 }
View Code
 1 @model MvcApplication18.Models.SecondHand_info
 2 @using MvcApplication18.Models
 3 @{
 4     ViewBag.Title = "Index";
 5 }
 6 
 7 @foreach (SecondHand_info item in ViewData["list"] as List<SecondHand_info>)
 8 {
 9   <p>@item.SecondHand_keyID </p> 
10      <p>@item.SecondHand_mobile </p> 
11      <p>@item.SecondHand_title </p> 
12      <p>@item.SecondHand_context </p> 
13   
14 }
15 <h2>Index</h2>
View Code

 5)带输出参数的存储过程

 1 CREATE PROCEDURE [dbo].[Myproc]
 2     @Name NVARCHAR(max),
 3     @PageIndex int,
 4     @PageSize INT,
 5     @TotalCount int OUTPUT
 6 as 
 7 
 8     declare @startRow int
 9     declare @endRow    int
10     
11     set @startRow = (@PageIndex - 1) * @PageSize + 1
12     set @endRow = @startRow + @PageSize - 1
13     
14     select *
15     FROM
16     (
17         select top (@endRow)
18            ID,
19             Age,
20             Name,
21             row_number() over(order by [ID] desc) as [RowIndex]
22         from dbo.Student
23     ) as T    
24     where [RowIndex] >= @startRow AND T.Name = @Name
25     
26     SET @TotalCount=(select count(1) as N 
27     FROM dbo.Student WHERE Name = @Name) 
View Code
1 var name = new SqlParameter { ParameterName = "Name", Value = Name };
2             var currentpage = new SqlParameter { ParameterName = "PageIndex", Value = currentPage };
3             var pagesize = new SqlParameter { ParameterName = "PageSize", Value = pageSize };
4             var totalcount = new SqlParameter { ParameterName = "TotalCount", Value = 0, Direction = ParameterDirection.Output };
5 
6             var list = ctx.Database.SqlQuery<Student>("Myproc @Name, @PageIndex, @PageSize, @TotalCount output",
7         name, currentpage, pagesize, totalcount);
8 
9             totalCount = (int)totalcount.Value;  /*获得要输出参数totalcount的值*/
View Code

 

posted @ 2015-09-04 01:21  每天进步一点点!  阅读(534)  评论(0编辑  收藏  举报