如何在不使用实体框架的情况下从mvc4中的数据库获取数据

编程入门 行业动态 更新时间:2024-10-26 04:27:24
本文介绍了如何在不使用实体框架的情况下从mvc4中的数据库获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是代码

This is code

[HttpPost] public ActionResult GetAllCompanies() { //public IEnumerable<CompanyDetails> GetCars() { using (var con = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString)) { var cmd = new SqlCommand("GetAllCompanies", con); cmd.CommandType = CommandType.StoredProcedure; try { if (con.State != ConnectionState.Open) con.Open(); cmd.ExecuteNonQuery(); return View(); } finally { if (con.State != ConnectionState.Closed) con.Close(); } } }

我的.cshtml页面中的 以下我没有进入foreach所有重新获得的数据...

in my .cshtml page below am not getting in foreach where all the retrived data will come ...

@using(Html.BeginForm()) { @foreach(var item in) { @ Html.DisplayFor(modelItem => item。) @ Html.DisplayFor(modelItem => item。) @ Html.DisplayFor(modelItem => ;项目。) @ Html.DisplayFor(modelItem => item。) @ Html.DisplayFor(modelItem => item。) @ Html.ActionLink(编辑,编辑)| @ Html.ActionLink(详细信息,详细信息)| @ Html.ActionLink (删除,删除) } } @using (Html.BeginForm()) { @foreach (var item in ) { @Html.DisplayFor(modelItem => item.) @Html.DisplayFor(modelItem => item.) @Html.DisplayFor(modelItem => item.) @Html.DisplayFor(modelItem => item.) @Html.DisplayFor(modelItem => item.) @Html.ActionLink("Edit", "Edit") | @Html.ActionLink("Details", "Details") | @Html.ActionLink("Delete", "Delete") } }

推荐答案

好的。你有三种方法: 1)使用其他ORM,而不是EF。那里有很多。此外:为什么不EF? - 存储过程不是问题,请参阅此 [ ^ ]。 2)创建结果集的模型类,基于该类的通用列表,并使用旧的ADO.NET工具将所有数据提取到该列表中。缺点:您必须立即获取所有内容,这可能会根据您的数据库变大。 3)在DataReader之上创建一个IEnumerable(参见这个 [ ^ ]),并将其作为模型传递给视图。 OK. You have three approaches: 1) Use an other ORM, not EF. There are plenty of them out there. Besides: why not EF? - stored procedure is not a problem, see this[^]. 2) Create a model class for your result set, a generic list based on that class, and fetch all data into that list with legacy ADO.NET tools. Drawback: you will have to fetch all at once, which can grow large depending on your database. 3) Create an IEnumerable on top of DataReader (see this[^]), and pass it to the View as model.

更多推荐

如何在不使用实体框架的情况下从mvc4中的数据库获取数据

本文发布于:2023-11-15 04:23:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   情况下   数据库   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!