ASP.NET MVC实现增删改查的一个demo

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/unclebober/article/details/85247350

首先我们要搞清楚什么是MVC?

http://dongcheng96.cn/index.php/2018/12/18/%E5%88%9D%E8%AF%86mvc/

首先在VS中新建一个ASP.NET web应用程序

先新建一个Model类User

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Test.Models
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Sex { get; set; }
        public int Age { get; set; }

    }
}

然后新建一个Controller控制器

代码如下:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Test.Models;

namespace Test.Controllers
{
    public class UserController : Controller
    {

        public const string constr = @"Data Source=SANDBOXV2DEV142;Initial Catalog=UserInfo;Integrated Security=True";
        // GET: User
        public ActionResult Index()
        {
            string text = "userinfo_Get"; //存储过程的名称
            SqlConnection conn = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(text, conn);
            cmd.CommandType = CommandType.StoredProcedure;//声明要执行的是存储过程
            conn.Open();    //Open the sql
            SqlDataAdapter da = new SqlDataAdapter(text, conn);
            DataSet ds = new DataSet();//dataset是一个数据集合,里面有很多的Table
            da.Fill(ds);

            List<Test.Models.User> userInfoList = new List<Models.User>();//新建一个集合来储存数据
            DataTable dt = ds.Tables[0];

            foreach (DataRow row in dt.Rows)
            {
                User user = new Models.User();
                user.Id= Convert.ToInt16(row["Id"]);
                user.Name =Convert.ToString(row["Name"]);
                user.Sex= Convert.ToString(row["Sex"]);
                user.Age = Convert.ToInt16(row["Age"]);

                userInfoList.Add(user);
            }

            conn.Close();
            return View(userInfoList);
        }
        
        public ActionResult AddInfo()
        {

            return View();
        }
        [HttpPost]
        public ActionResult AddInfo1()
        {
            string text = "userinfo_add";
            var test = Request.Form["UserName"].ToString();
            SqlParameter[] addpara =
            {
                new SqlParameter("@name",Request.Form["UserName"].ToString()),
                new SqlParameter("@sex",Request.Form["UserSex"].ToString()),
                new SqlParameter("@age",Convert.ToInt16(Request.Form["UserAge"]))
            };

            SqlConnection conn = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(text, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter parameter in addpara)
            {
                if (parameter != null)
                    cmd.Parameters.Add(parameter);
                else
                    cmd.Parameters.Add(0);
            }
            conn.Open();    //Open the sql

            cmd.ExecuteNonQuery();//将数据存入数据库,这一步必不可少
            conn.Close();
            return View();
        }

        public ActionResult DelInfo(int id)
        {
            string text = "userinfo_del";
            var test = id;
            SqlParameter[] delpara = {
                new SqlParameter("@id",id)
            };
            SqlConnection conn = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(text, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter parameter in delpara)
            {
                cmd.Parameters.Add(parameter);
            }
            conn.Open();    //Open the sql
            cmd.ExecuteNonQuery();
            conn.Close();
            return View();
        }

        public ActionResult UpdateInfo(int id)
        {
            return View();
        }
        [HttpPost]
        public ActionResult UpdateInfo1(int id)
        {
            string text = "userinfo_update";
            var test = Request.Form["UserName"].ToString();
            SqlParameter[] updatepara =
            {
                new SqlParameter("@id",id),
                new SqlParameter("@name",Request.Form["UserName"].ToString()),
                new SqlParameter("@sex",Request.Form["UserSex"].ToString()),
                new SqlParameter("@age",Convert.ToInt16(Request.Form["UserAge"]))
            };
            //updatepara[0].Value=

            SqlConnection conn = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(text, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter parameter in updatepara)
            {
                if (parameter != null)
                    cmd.Parameters.Add(parameter);
                else
                    continue;
            }
            conn.Open();    //Open the sql
            cmd.ExecuteNonQuery();
            conn.Close();
            return View();
        }

        [HttpPost]
        public ActionResult SelInfo()
        {
            string text = "userinfo_sel";
            var test = Request.Form["SelInfo"].ToString();
            SqlParameter[] selpara =
            {
                new SqlParameter("@name",Request.Form["SelInfo"].ToString())
            };
            SqlConnection conn = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(text, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();    //Open the sql
            foreach (SqlParameter parameter in selpara)
            {
                cmd.Parameters.Add(parameter);
            }
            SqlDataReader reader=cmd.ExecuteReader();

            List<User> list = new List<User>();
            while(reader.Read())
            {
                User user = new User()
                {
                    Name = reader["Name"].ToString(),
                    Sex = reader["Sex"].ToString(),
                    Age = Convert.ToInt16(reader["Age"])
                };
                list.Add(user);
            }
            return View(list);
        }
    }

}

View页面用的是MVC的模板

展开阅读全文

没有更多推荐了,返回首页