存储过程在ASP.NET MVC中

时间:2018-06-12 09:24:59

标签: asp.net-mvc-5

**stored procedure**

如何在mvc中调用此过程 **请解释**

CREATE procedure [dbo].[RegistrationInsert](  
@empEmail nvarchar(max),  
@password nvarchar (max)  
)  
as begin  
insert into [Registration ] ([EmployeeName],[EmployeeEmail],[Password],[PhoneNumber],[Role],[EmployeeID])  
select [EmployeeName],[EmployeeMailID],@password,[EmployeePhoneNumber],[EmployeeDesignation],[EmployeeID]  
from [Employeedata ]  
where [EmployeeMailID] = @empEmail  
end  
GO 

2 个答案:

答案 0 :(得分:1)

如果我们想要使用存储过程,则需要使用非常安全,顺畅和高效的Code First Migrations。因此,请使用以下步骤。

第1步

<强> 启用-迁移

第2步

现在数据上下文类将使用存储过程。打开Context类并更新代码,如下所示:

打开工具 - &gt; NuGet包管理器 - &gt;包管理器控制台并输入以下命令:

namespace MvcStoredProcedureSample.Models  
{  
    public class MovieDbContext : DbContext  
    {  
        public MovieDbContext() : base( "name = MovieDbContext" )  
        {  
        }  

        public DbSet<Movie> Movies { get; set; }  

        protected override void OnModelCreating(DbModelBuilder modelBuilder)  
        {  
            modelBuilder.Entity<Movie>().MapToStoredProcedures();  
        }  

    }  
}

第3步

using System.Data.Entity.Migrations;  

    public partial class MyMovieSP : DbMigration  
    {  
        public override void Up()  
        {  
            CreateStoredProcedure(  
                "dbo.Movie_Insert",  
                p => new  
                    {  
                        Name = p.String(),  
                        ReleaseDate = p.DateTime(),  
                        Category = p.String(),  
                    },  
                body:  
                    @"INSERT [dbo].[Movies]([Name], [ReleaseDate], [Category])  
                      VALUES (@Name, @ReleaseDate, @Category)  

                      DECLARE @ID int  
                      SELECT @ID = [ID]  
                      FROM [dbo].[Movies]  
                      WHERE @@ROWCOUNT > 0 AND [ID] = scope_identity()  

                      SELECT t0.[ID]  
                      FROM [dbo].[Movies] AS t0  
                      WHERE @@ROWCOUNT > 0 AND t0.[ID] = @ID"  
            );
        }
public override void Down()  
        {   
            DropStoredProcedure("dbo.Movie_Insert");  
        } 
}

构建解决方案。现在,在Package Manager控制台中输入以下命令:

添加迁移MyMovieSP

您可以在MyMovieSP的位置使用任何名称。

第4步

我们需要告诉数据库创建MyMovieSP。因此,只需在程序包管理器控制台中输入以下命令,

<强> 更新的数据库的

为了更好地理解,请访问完整文章 using Stored Procedure In ASP.NET MVC

答案 1 :(得分:0)

步骤1:创建视图模型(注册)

public class Register 
{
    [Display(Name = "E-mail")]
    [Remote("IsUserNameAvailable", "User", ErrorMessage = "Email Already 
     Exists")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Email is Required")]
    public string EmployeeEmail {get;set;}

    [Required(AllowEmptyStrings = false, ErrorMessage = "Password is 
    Required")]
    [Display(Name = "Password")]
    [MinLength(6, ErrorMessage = "Minimum 6 Characters")]
    public string Password { get; set; }
}

步骤2:从数据库添加ADO.Net EntityModel。

添加表格和存储过程

Adding tables and stored procedures from database

Open Modal browser and see sp(RegistrationInsert)

步骤3:为数据库创建对象,如

Database db = new Database();

public ActionResult Register(Register view)
{
    Database db = new Database();
    string message = "";
    db.RegistrationInsert(empEmail: view.EmployeeEmail, password: 
    view.Password);
    ViewBag.Message = "Registered Successfully";
    return View();
} 

第4步:创建Razorview:

Razorview