美好的一天,我在这里有点冷静.我使用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方法返回包含视图模型的视图.