Managing Data Insertion to SQL Server in MVC 5 Project with Stored Procedure Integration

These codes manage an HTTP Post operation to add a record to an SQL Server database by creating a Controller and Model in an MVC 5 project. The Controller receives the HTTP Post request, performs necessary validations, and then invokes a stored procedure in SQL Server using the data received through the Model. The Model contains validation attributes to ensure the correctness of the received data. The stored procedure adds a new personnel record to the database. By using these codes, you can perform a basic database operation in an MVC 5 project.

These codes manage an HTTP Post operation to add a record to an SQL Server database by creating a Controller and Model in an MVC 5 project. The Controller receives the HTTP Post request, performs necessary validations, and then invokes a stored procedure in SQL Server using the data received through the Model. The Model contains validation attributes to ensure the correctness of the received data. The stored procedure adds a new personnel record to the database. By using these codes, you can perform a basic database operation in an MVC 5 project.

SQL Server Stored Procedure:

CREATE PROCEDURE sp_InsertPersonel
    @Adı NVARCHAR(50),
    @Soyadı NVARCHAR(50),
    @Departman NVARCHAR(50),
    @Pozisyon NVARCHAR(50)
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO Personel (Adı, Soyadı, Departman, Pozisyon)
    VALUES (@Adı, @Soyadı, @Departman, @Pozisyon);
END

This stored procedure can be used to add a new record to the Personnel table. Now let's write the Controller and Model sections in the MVC 5 project.

MVC 5 Controller:

using System.Web.Mvc;
using System.Data.SqlClient;

namespace YourNamespace.Controllers
{
    public class PersonelController : Controller
    {
        // GET: Personel
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(PersonelModel personel)
        {
            if (ModelState.IsValid)
            {
                // Veritabanına bağlantı dizesi
                string connectionString = "your_connection_string_here";

                // Stored procedure çağrısını yap
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    SqlCommand command = new SqlCommand("sp_InsertPersonel", connection);
                    command.CommandType = System.Data.CommandType.StoredProcedure;

                    // Parametreleri ekle
                    command.Parameters.AddWithValue("@Adı", personel.Adı);
                    command.Parameters.AddWithValue("@Soyadı", personel.Soyadı);
                    command.Parameters.AddWithValue("@Departman", personel.Departman);
                    command.Parameters.AddWithValue("@Pozisyon", personel.Pozisyon);

                    // Komutu çalıştır
                    command.ExecuteNonQuery();
                }

                return RedirectToAction("Index");
            }

            return View(personel);
        }
    }
}

Personel Modeli:

using System.ComponentModel.DataAnnotations;

namespace YourNamespace.Models
{
    public class PersonelModel
    {
        [Required(ErrorMessage = "Ad alanı gereklidir.")]
        public string Adı { get; set; }

        [Required(ErrorMessage = "Soyad alanı gereklidir.")]
        public string Soyadı { get; set; }

        [Required(ErrorMessage = "Departman alanı gereklidir.")]
        public string Departman { get; set; }

        [Required(ErrorMessage = "Pozisyon alanı gereklidir.")]
        public string Pozisyon { get; set; }
    }
}

These code snippets manage HTTP Post operations by creating a Controller and Model in an MVC 5 project. You'll need to add a connection string to connect to the database and update it appropriately.