stored-procedures – 如何在ASP.Net MVC(C#)中调用和执行存储过
美好的一天,我在这里有点冷静.我使用ASP.NET MVC和C#在visual studio中创建了我的数据库,模型,控制器和视图,但我无法弄清楚如何调用我创建的存储过程.
我希望在我放在视图中的按钮上调用存储过程. 这是我的“员工”模型: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Web; namespace MVCSimpleApp.Models { [Table("Employees")] public class Employee { [Display(Name ="Employee Id")] public int EmployeeId { get; set; } [Display(Name ="First Name")] public string FirstName { get; set; } [Display(Name ="Last Name")] public string LastName { get; set; } } } 这是我的数据上下文: using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; namespace MVCSimpleApp.Models { public class EmployeeContext : DbContext { public DbSet<Employee> Employee { get; set; } } } 这是我的员工控制员: using MVCSimpleApp.Models; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; namespace MVCSimpleApp.Controllers { public class EmployeeController : Controller { private EmployeeContext db = new EmployeeContext(); // GET: Employee public ActionResult Index() { var employees = from e in db.Employee select e; return View(employees); } } } 现在这是我的存储过程.它并不多,只是出于实践目的. Create Proc DisplayStudents AS BEGIN /*selecting all records from the table whose name is "Employee"*/ Select * From Employee END 这是我的看法: @model IEnumerable<MVCSimpleApp.Models.Employee> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Student List</h2> <p> <a href="@Url.Action("Create")" title="Add new" class="btn btn-primary btn-lg"> <span class="glyphicon glyphicon-plus "></span> Add Student </a> </p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.EmployeeId) </th> <th> @Html.DisplayNameFor(model => model.FirstName) </th> <th> @Html.DisplayNameFor(model => model.LastName) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(model => item.EmployeeId) </td> <td> @Html.DisplayFor(modelItem => item.FirstName) </td> <td> @Html.DisplayFor(modelItem => item.LastName) </td> <td> <span> <a href="@Url.Action("Edit",new { id = item.EmployeeId})" title="Edit Record"> <span class="glyphicon glyphicon-pencil"></span> </a> </span> | <span> <a href="@Url.Action("Details",new { id = item.EmployeeId})" title="View Details"> <span class="glyphicon glyphicon-th-list"></span> </a> </span> | <span> <a href="@Url.Action("Delete",new { id = item.EmployeeId})" title="Delete"> <span class="glyphicon glyphicon-trash"></span> </a> </span> </td> </tr> } /*this is the button I want the stored procedure to be called on when I click it*/ <button>Run</button> </table> 请大家,我需要您就此事提出意见和反馈.将接受将参数传递给存储过程的提示.如果我不在这里做事,请纠正我.感谢你的关心. 解决方法
如果使用EF不是必需的,您可以通过以下方式进行:
string cnnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString; SqlConnection cnn = new SqlConnection(cnnString); SqlCommand cmd = new SqlCommand(); cmd.Connection = cnn; cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = "ProcedureName"; //add any parameters the stored procedure might require cnn.Open(); object o = cmd.ExecuteScalar(); cnn.Close(); 如果您需要使用Entity Framework,请查看this discussion.此外,您还希望使用存储过程进行插入,更新和删除,从Microsoft查看this tutorial. 要从按钮单击执行代码,您可以在表单中创建一个表单,只需一个按钮,如下所示: @using(Html.BeginForm("TestAction","TestController",FormMethod.Get)) { <input type="submit" value="Submit" /> } 在您的控制器中,您将拥有这样的TestAction方法 public ActionResult TestAction(){....} 如果需要将任何参数传递给TestAction,只需将它们指定为方法中的参数,然后使用接受actionName,controllerName,routeValues和formMethod作为参数的重载版本的BeginForm. 要将结果传递给视图,您需要根据从存储过程中接收的值创建具有属性的视图模型,然后从TestAction方法返回包含视图模型的视图. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 编辑器在没有@foreach的情况下不渲染可枚举
- ASP.Net CMS推荐,果园,Sitefinity,Umbraco或N2?
- 如何为ASP.NET WebAPI 2中使用路由属性的特定控制器添加Mes
- asp.net – 将JSON发布到Controller
- CookieAuthenticationOptions,ExpireTimeSpan不起作用
- asp.net – ELMAH登录SQL Server
- asp.net-mvc – 从FormCollection元素获取多个复选框
- .NET HttpSessionState案例不敏感
- 在ASP.Net中加载用户控件并处理回发
- asp.net-mvc – 如何处理ASP.NET MVC中动态生成的表单的输出
- asp.net-mvc-3 – 应该如何看待“分离”?
- asp.net-mvc – 提交表单并将数据传递给FileStre
- asp.net-mvc – 从视图重定向到另一个视图
- asp.net-mvc – MVC和NOSQL:直接将视图模型保存
- asp.net-mvc – 如何从ASP.NET MVC VIEWS文件夹访
- 如何在ASP.Net webform中使用标签?
- asp.net-mvc – EF,ASP MVC依赖注入.多个并发请求
- asp.net-mvc – ASP.Net MVC 5带范围的Google身份
- 单声道是asp.net的可行替代方案吗?
- asp.net-core – appsettings.json中ConnectionS