CRUD operations in MVC using stored procedure without Entity Framework

In MVC, CRUD operations can be performed using stored procedures without using Entity Framework. Here’s a detailed explanation with examples:

Create (INSERT) Operation:

CREATE PROCEDURE [dbo].[sp_InsertData]
    @Name VARCHAR(50),
    @Age INT,
    @Email VARCHAR(100)
AS
BEGIN
    INSERT INTO [dbo].[UserData] ([Name], [Age], [Email])
    VALUES (@Name, @Age, @Email)
END
  

In this example, we have a stored procedure called “sp_InsertData” that takes three parameters – Name, Age, and Email. It inserts a new record into the “UserData” table with the provided values.

Read (SELECT) Operation:

CREATE PROCEDURE [dbo].[sp_GetUserData]
    @Id INT
AS
BEGIN
    SELECT [Name], [Age], [Email]
    FROM [dbo].[UserData]
    WHERE [Id] = @Id
END
  

This stored procedure called “sp_GetUserData” retrieves data from the “UserData” table based on the provided Id. It returns the Name, Age, and Email of the record matching the given Id.

Update (UPDATE) Operation:

CREATE PROCEDURE [dbo].[sp_UpdateData]
    @Id INT,
    @Name VARCHAR(50),
    @Age INT,
    @Email VARCHAR(100)
AS
BEGIN
    UPDATE [dbo].[UserData]
    SET [Name] = @Name,
        [Age] = @Age,
        [Email] = @Email
    WHERE [Id] = @Id
END
  

The above stored procedure named “sp_UpdateData” updates the Name, Age, and Email of the record in the “UserData” table based on the provided Id.

Delete (DELETE) Operation:

CREATE PROCEDURE [dbo].[sp_DeleteData]
    @Id INT
AS
BEGIN
    DELETE FROM [dbo].[UserData]
    WHERE [Id] = @Id
END
  

Lastly, the stored procedure called “sp_DeleteData” deletes the record from the “UserData” table based on the provided Id.

These stored procedures can be executed in your MVC application using the ADO.NET library or any other database access technology to perform the CRUD operations without using Entity Framework.

Same cateogry post

Leave a comment