2016-09-19 53 views
4

美好的一天傢伙,我在這裏有一點點冷漠。我已經使用ASP.NET MVC和C#在Visual Studio中創建了我的數據庫,模型,控制器和視圖,但我無法弄清楚如何調用我創建的存儲過程。如何在ASP.Net中調用和執行存儲過程MVC(C#)

我想讓存儲過程在我放入視圖的按鈕上調用。 該存儲過程應該在單擊該按鈕時執行並顯示結果。 下面是我創建的存儲過程,視圖,模型和控制器。

這是我的 '員工' 型號:

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> 

請傢伙,我需要在這個問題上你的意見和反饋。將接受將參數傳遞給存儲過程的提示。如果我沒有在這裏做事,請糾正我。感謝你的關心。

+0

有噸的在互聯網上的例子 - 這個看起來很不錯 - http://csharp-station.com/Tutorial/AdoDotNet/Lesson07 – 2016-09-19 14:59:25

+0

感謝的人,現在檢查出來。 –

+0

你可以去這個,希望它會幫助你[答案](http:// stackoverflow。com/questions/34761857/mvc-select-from-stored-procedure-in-entity-framework/34762768#34762768) – Viplock

回答

3

如果使用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(); 

如果您需要使用實體框架退房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方法的視圖模型返回視圖。

+0

不錯的一個@Endi,感謝理解我不想用EF做這個,但是怎麼做我在ASP.NET MVC中的按鈕單擊事件上執行該操作嗎?準確地說,一個放在視圖上的按鈕?無法弄清楚如何在Web窗體中完成代碼。 –

+0

Moreove @Endi,我如何將過程的結果返回到視圖? –

+0

我編輯我的帖子來回答你的問題 –

1
try 
{  
    conn.Open(); 
    SqlCommand dCmd = new SqlCommand("store_procedure_name",conn); 
    dCmd.CommandType = CommandType.StoredProcedure; 
    dCmd.Parameters.Add(new SqlParameter("@parameter2",parameter2)); 
    dCmd.Parameters.Add(new SqlParameter("@parameter1", parameter1)); 
    SqlDataAdapter da = new SqlDataAdapter(dCmd); 
    DataTable table = new DataTable(); 
    ds.Clear(); 

    da.Fill(ds); 
    conn.Close(); 

    var das = ds.Tables[0].AsEnumerable(); 
    return ConvertToDictionary(ds.Tables[0]); 
} 
catch 
{ 

} 
+1

請避免代碼唯一的答案,並嘗試解釋_爲什麼OP應該使用此代碼。 –

+0

當然,我會做充分的解釋 –

+0

看起來像從未發生過的解釋 – Mike