using simple queries in ASP.NET MVC

后端 未结 4 1508
遇见更好的自我
遇见更好的自我 2020-12-29 16:03

I checked google but found nothing good. I am searching for usinf Traditional SQL queries in MVC instead of Entity framework etc. So it would be go

4条回答
  •  星月不相逢
    2020-12-29 16:42

    Simplest example:

    //Domain Class
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    
    namespace BanjoOnMyKnee.Models
    {
        public class DomainModel
        {
            public string connectionString = ".\\SQLEXPRESS; Initial-Catalog=YourDBName; Integrated-Security=true";
            public void CreateSomething(ViewModel model)
            {
                using(SqlConnection connection = new SqlConnection(connectionString))
                using (SqlCommand command = new SqlCommand("",connection))
                {
                    command.CommandText = "insert into Names values(@Name)";
                    command.Parameters.AddWithValue("@Name", model.Name);
                    command.ExecuteNonQuery();
                }
            }
    
            public ViewModel FindSomething(int id)
            {
                var model = new ViewModel();
                using (SqlConnection connection = new SqlConnection(connectionString))
                using (SqlCommand command = new SqlCommand("", connection))
                {
                    command.CommandText = "select * from Names where Id=@Id";
                    command.Parameters.AddWithValue("@Id",id);
                    SqlDataReader reader = command.ExecuteReader();
                    model.Id = id;
                    model.Name = reader["Name"].ToString();
                }
                return model;
            }
    
            public void DeleteSomething(ViewModel model)
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                using (SqlCommand command = new SqlCommand("", connection))
                {
                    command.CommandText = "delete from Names where Id=@Id";
                    command.Parameters.AddWithValue("@Id", model.Id);
                    command.ExecuteNonQuery();
                }
            }
    
            public void EditSomething(ViewModel model)
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                using (SqlCommand command = new SqlCommand("", connection))
                {
                    command.CommandText = "Update Names set Name=@Name where Id=@Id";
                    command.Parameters.AddWithValue("@Name", model.Name);
                    command.Parameters.AddWithValue("@Id", model.Id);
                    command.ExecuteNonQuery();
                }
            }
        }
    }
    

    And here's my controller class

    //My Controller class
    public class HomeController : Controller
    {
        //
        // GET: /Home/
    
        public ActionResult Index()
        {
            return View();
        }
    
        //
        // GET: /Home/Create
    
        public ActionResult Create()
        {
            return View(new ViewModel());
        }
    
        //
        // POST: /Home/Create
    
        [HttpPost]
        public ActionResult Create(ViewModel vm)
        {
            try
            {
                var domainModel = new DomainModel();
                domainModel.CreateSomething(vm);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(new ViewModel());
            }
        }
    
        //
        // GET: /Home/Edit/5
    
        public ActionResult Edit(int id)
        {
            ViewModel model = new DomainModel().FindSomething(id);
            return View(model);
        }
    
    
        [HttpPost]
        public ActionResult Edit(ViewModel editModel)
        {
            try
            {
                var dm = new DomainModel();
                dm.EditSomething(editModel);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(new ViewModel());
            }
        }
     }
    

    My ViewModel class

    //My ViewModel
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace BanjoOnMyKnee.Models
    {
        public class ViewModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
    

    And my 'Create' View

    //My view
    @model BanjoOnMyKnee.Models.ViewModel
    
    @{
        ViewBag.Title = "Create";
    }
    
    

    Create

    @using(Html.BeginForm()){ @Html.HiddenFor(m => m.Id);

    Name : Html.EditorFor(m=>m.Name);

    }

提交回复
热议问题