MVC存储过程

public class GradeDal
    {
        public int AddGrade(Grade Grade)
        {
            int Result = 0;
            using (SqlConnection scon = new SqlConnection("Server=.;uid=sa;pwd=1234;database=StudyQutionDB"))
            {
                SqlCommand scom = new SqlCommand("AddGrade", scon);
                scom.CommandType = System.Data.CommandType.StoredProcedure; // 指定命令类型为存储过程
                scom.Parameters.Add(new SqlParameter("@Name", Grade.Name)); // Parameters 参数集合
                scom.Parameters.Add(new SqlParameter("@Remark", Grade.Remark));
                scon.Open();
                Result = scom.ExecuteNonQuery();
                scon.Close();
            }
            return Result;
        }

      

        public int UpdateGrade(Grade Grade)
        {
            int Result = 0;
            using (SqlConnection scon = new SqlConnection("Server=.;uid=sa;pwd=1234;database=StudyQutionDB"))
            {
                SqlCommand scom = new SqlCommand("UpdateGrade", scon);
                scom.CommandType = System.Data.CommandType.StoredProcedure; // 指定命令类型为存储过程
                scom.Parameters.Add(new SqlParameter("@Name", Grade.Name)); // Parameters 参数集合
                scom.Parameters.Add(new SqlParameter("@Remark", Grade.Remark));
                scom.Parameters.Add(new SqlParameter("@Id", Grade.Id));
                scon.Open();
                Result = scom.ExecuteNonQuery();
                scon.Close();
            }
            return Result;
        }

        public int DelGrade(int Id)
        {
            int Result = 0;
            using (SqlConnection scon = new SqlConnection("Server=.;uid=sa;pwd=1234;database=StudyQutionDB"))
            {
                SqlCommand scom = new SqlCommand("DelGrade", scon);
                scom.CommandType = System.Data.CommandType.StoredProcedure; // 指定命令类型为存储过程
                scom.Parameters.Add(new SqlParameter("@Id", Id));// Parameters 参数集合
                scon.Open();
                Result = scom.ExecuteNonQuery();
                scon.Close();
            }
            return Result;
        }


        public List<Grade> GetAll()
        {
            List<Grade> list = new List<Grade>();
            using (SqlConnection scon = new SqlConnection("Server=.;uid=sa;pwd=1234;database=StudyQutionDB"))
            {
                SqlCommand scom = new SqlCommand("GetGrade", scon);
                scom.CommandType = System.Data.CommandType.StoredProcedure; // 指定命令类型为存储过程
                scon.Open();
                SqlDataReader reader = scom.ExecuteReader();
                while (reader.Read())
                {
                    Grade grade = new Grade();
                    grade.Id = (int)reader["Id"];
                    grade.Name = (string)reader["Name"];
                    grade.Remark = (string)reader["Remark"];
                    list.Add(grade);
                }
                reader.Close();
                scon.Close();
            }
            return list;
        }

        public Grade GetGradeById(int Id)
        {
            Grade grade = new Grade();
            using (SqlConnection scon = new SqlConnection("Server=.;uid=sa;pwd=1234;database=StudyQutionDB"))
            {
                SqlCommand scom = new SqlCommand("GetGradeById", scon);
                scom.CommandType = System.Data.CommandType.StoredProcedure; // 指定命令类型为存储过程
                scom.Parameters.Add(new SqlParameter("@Id", Id));// Parameters 参数集合
                scon.Open();
                SqlDataReader reader = scom.ExecuteReader();
                if (reader.Read())
                {
                   
                    grade.Id = (int)reader["Id"];
                    grade.Name = (string)reader["Name"];
                    grade.Remark = (string)reader["Remark"];
                }
                reader.Close();
                scon.Close();
            }
            return grade;
        }
    }
//MVC视图
 public class ShitiController : Controller
    {
        HttpClientHelper helper = new HttpClientHelper("http://localhost:65268/");
        // GET: Shiti
        public ActionResult Index(int pageindex=1,int pagesize=2)
        {
            string sql = helper.Get($"api/Shiti/Get?pageindex={pageindex}&pagesize={pagesize}");
            Shiti[] a = JsonConvert.DeserializeObject<List<Shiti>>(sql).ToArray();
            int RowCount = (a == null ? 0 : a[0].pagecount);
            int Count=(int)Math.Ceiling((decimal)RowCount/pagesize);
            ViewBag.pagepre = pageindex <= 1 ? 1 : pageindex - 1;
            ViewBag.pagenext = pageindex <= Count ? Count : pageindex + 1;
            ViewBag.pagelast = Count;
            return View(a.ToList());
        }

       
        public ActionResult Add()
        {

            return View();

        }
        [HttpPost]
        public void Add(Shiti s)
        {
            s.Ri = DateTime.Now;
            string sql = helper.Post("api/Shiti/Post", JsonConvert.SerializeObject(s));
            int i = int.Parse(sql);
            if (i > 0)
            {
                Response.Write("<script>alert('添加成功');location.href='/Shiti/Index'</script>");
            }
            else
            {

                Response.Write("<script>alert('添加失败');location.href='/Shiti/Add'</script>");
            }
        }
        public void Delete(int id)
        {
            string sql = helper.Delete("api/Shiti/Delete/" + id);
            int a = int.Parse(sql);
            if (a > 0)
            {
                Response.Write("<script>alert('删除成功');location.href='/Shiti/Index'</script>");

            }
            else
            {
                Response.Write("<script>alert('删除失败');location.href='/Shiti/Index'</script>");

            }
        }
        public ActionResult Upt(int id)
        {
            string sql = helper.Get("api/Shiti/Upt/" + id);
            List<Shiti> list = JsonConvert.DeserializeObject<List<Shiti>>(sql);
            return View(list.FirstOrDefault());
        }
        [HttpPost]
        public void Upt(Shiti s)
        {
            string sql = helper.Post("api/Shiti/Update", JsonConvert.SerializeObject(s));
            int i = int.Parse(sql);

            if (i > 0)
            {
                Response.Write("<script>alert('修改成功');location.href='/Shiti/Index'</script>");

            }
            else
            {
                Response.Write("<script>alert('修改失败');location.href='/Shiti/Update'</script>");

            }

        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值