标签 asp.net 下的文章

asp.net mvc中EF执行带输出参数的存储过程

public ActionResult StoredProcedure(int id = 1)
{
using (var db = new DataContext())
{
var pageSize = 5;
var startIndex = (id - 1)*pageSize + 1;
SqlParameter prmStart = new SqlParameter("@startIndex", startIndex);
SqlParameter prmEnd = new SqlParameter("@endIndex", startIndex + pageSize);
SqlParameter prmTotal = new SqlParameter("@totalItems", SqlDbType.Int);
prmTotal.Direction = ParameterDirection.Output;

          var articles =  
              db.Database.SqlQuery<Article>(  
                  "exec [USP_GetPagedArticleList] @startIndex,@endIndex,@totalItems out", prmStart, prmEnd,  
                  prmTotal).ToList();  
          var totalItems = (int) prmTotal.Value;  
          var model = new PagedList<Article>(articles, id, pageSize, totalItems);  
          return View(model);  
      }  
  }