Nat*_*afa 6 c# stored-procedures visual-studio-2015
美好的一天,我在这里有点冷静.我使用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; }
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的数据上下文:
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的员工控制员:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在这是我的存储过程.它并不多,只是出于实践目的.
Create Proc DisplayStudents
AS
BEGIN
/*selecting all records from the table whose name is "Employee"*/
Select * From Employee
END
Run Code Online (Sandbox Code Playgroud)
这是我的看法:
@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>
Run Code Online (Sandbox Code Playgroud)
请大家,我需要您就此事提出意见和反馈.将接受将参数传递给存储过程的提示.如果我不在这里做事,请纠正我.感谢你的关心.
End*_*ani 11
如果使用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();
Run Code Online (Sandbox Code Playgroud)
如果您需要使用Entity Framework,请查看此讨论.您还想使用存储过程进行插入,更新和删除,请查看Microsoft的本教程.
要从按钮单击执行代码,您可以在表单中创建一个表单,只需一个按钮,如下所示:
@using(Html.BeginForm("TestAction", "TestController", FormMethod.Get))
{
<input type="submit" value="Submit" />
}
Run Code Online (Sandbox Code Playgroud)
在您的控制器中,您将拥有这样的TestAction方法
public ActionResult TestAction(){....}
Run Code Online (Sandbox Code Playgroud)
如果需要将任何参数传递给TestAction,只需将它们指定为方法中的参数,然后使用接受actionName,controllerName,routeValues和formMethod作为参数的重载版本的BeginForm.
要将结果传递给视图,您需要根据从存储过程中接收的值创建具有属性的视图模型,然后从TestAction方法返回包含视图模型的视图.
| 归档时间: |
|
| 查看次数: |
65222 次 |
| 最近记录: |