初学.net增删改查

分页显示

DAL:

public List GetListByPager(int PageIndex, int PageSize, out int RowCount)

  {

    string sql = "select top " + PageSize + " * from GTMessage where GId not in (select top ((" + PageIndex + "-1)*" + PageSize + ") GId from GTMessage)";

    string sqll = "select count(*) from GTMessage";

     RowCount = Convert.ToInt32(DBHelper.ExecuteScalar(sqll));

    DataTable dt = DBHelper.GetTable(sql);

     List list = new List();

     for (int i = 0; i < dt.Rows.Count; i++)

     {

      GModel g = new GModel();

      g.GId = Convert.ToInt32(dt.Rows[i]["GId"]);

      g.GCarId = dt.Rows[i]["GCarId"].ToString();

      g.GHomeId = dt.Rows[i]["GHomeId"].ToString();

      g.GGoods = dt.Rows[i]["GGoods"].ToString();

      g.GTime = dt.Rows[i]["GTime"].ToString();

      g.GName = dt.Rows[i]["GName"].ToString();

      list.Add(g);

    }

    return list;

  }

Controllers:

public ActionResult Index()
        {
            int PageIndex = 1;
            int PageSize = 5;
            int RowCount = 0;
            int PageCount = 0;
            if (Request["PageIndex"] != null)
            {
                PageIndex = Convert.ToInt32(Request["PageIndex"]);
            }

            if (PageIndex < 1)
            {
                PageIndex = 1;
            }
            if (Session["PageCount"] != null && PageIndex > Convert.ToInt32(Session["PageCount"]))
            {
                PageIndex = Convert.ToInt32(Session["PageCount"]);
            }


            ViewData["PIndex"] = PageIndex;
            List<GModel> list = bll.GetListByPager(PageIndex, PageSize, out RowCount);

            PageCount = RowCount % PageSize == 0 ? RowCount / PageSize : RowCount / PageSize + 1;
            Session["PageCount"] = PageCount;
            return View(list);
        }

View:

<a href="/Home/Index/1">首页</a>
        <a href="/Home/Index?PageIndex=@(Convert.ToInt32(ViewData["PIndex"])-1)">上一页</a>

        @for (int ii = 0; ii < Convert.ToInt32(Session["PageCount"]); ii++)
        {
            if ((ii + 1) == Convert.ToInt32(ViewData["PIndex"]))
            {
                <a href="/Home/Index?PageIndex=@(ii+1)" style="font-size:20px;color:red">@(ii + 1)</a>
            }
            else
            {
                <a href="/Home/Index?PageIndex=@(ii+1)">@(ii + 1)</a>
            }
        }



        <a href="/Home/Index?PageIndex=@(Convert.ToInt32(ViewData["PIndex"]) + 1)">下一页</a>
        <a href="/Home/Index?PageIndex=@Session["PageCount"]">尾页</a>

添加

 DAL:

public int AddGaoTie(GModel GModel)
        {
            string sql = "insert into GTMessage values ('" + GModel.GCarId + "','" + GModel.GHomeId + "','" + GModel.GGoods + "','" + GModel.GTime + "','" + GModel.GName + "')";
            return DBHelper.ExecuteNonQuery(sql);
        }

Controllers:

public ActionResult AddDo(GModel GModel)
        {
            int i = bll.AddGaoTie(GModel);
            if (i > 0)
            {
                return Content("<script>alert('添加成功!');location.href='/Home/index'</script>");
            }
            else
            {
                return Content("<script>alert('添加失败!');location.href='/Home/Add'</script>");
            }
        }

修改

修改首先要根据id查询单行数据进行修改视图的反填

根据id查询单行数据DAL:

public GModel GetListById(int id)
        {
            string sql = "select * from GTMessage where Gid="+id+"";

            DataTable table = DBHelper.GetTable(sql);
            GModel g = new GModel();


            g.GId = Convert.ToInt32(table.Rows[0]["GId"]);
            g.GCarId = (table.Rows[0]["GCarId"]).ToString();
            g.GGoods = (table.Rows[0]["GGoods"]).ToString();
            g.GHomeId = (table.Rows[0]["GHomeId"]).ToString();
            g.GName = (table.Rows[0]["GName"]).ToString();
            g.GTime = (table.Rows[0]["GTime"]).ToString();


            return g;

        }

执行修改DAL:

public int UpdGaoTie(GModel GModel)
        {
            string sql = "update GTMessage set GCarId=" + GModel.GCarId + ",GHomeId=" + GModel.GHomeId + ",GGoods=" + GModel.GGoods + ",GTime=" + GModel.GTime + ",GName=" + GModel.GName + " where GId=" + GModel.GId + "";
            return DBHelper.ExecuteNonQuery(sql);
        }

Controllers:

修改视图反填:

public ActionResult UpdateShow(int id)
        {
            GModel g = bll.GetListById(id);
                
            return View(g);
        }

执行修改:

public ActionResult UpdDo(GModel GModel)
        {
            int i = bll.UpdGaoTie(GModel);
            if (i > 0)
            {
                return Content("<script>alert('修改成功!');location.href='/Home/index'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败!');location.href='/Home/UpdateShow'</script>");
            }
        }

修改视图中数据id可以不显示,但视图中必须获取id数据,可以用@Html.Hidden("GId")来隐藏id的显示。因为在做修改操作时须将id传给Controllers的执行修改中进行修改

删除

DAL

 public int DelGaoTie(string ids)
        {
            string sql = "delete GTMessage where GId in (" + ids + ")";
            return DBHelper.ExecuteNonQuery(sql);
        }

Controllers:

public ActionResult DelDo(int id)
        {
            int i = bll.DelGaoTie(id.ToString());
            if (i > 0)
            {
                return Content("<script>alert('删除成功!');location.href='/Home/index'</script>");
            }
            else
            {
                return Content("<script>alert('删除失败!');location.href='/Home/index'</script>");
            }
        }

根据GGoods,GName查询数据

public List<GModel> SearchistByPager(int PageIndex, int PageSize, string GGoods, string GName)
        {
            string sql = "select top " + PageSize + " * from GTMessage where GId not in (select top ((" + PageIndex + "-1)*" + PageSize + ") GId  from GTMessage) and GGoods like '%" + GGoods + "%' and GName like '%" + GName+ "%'";
            //string sqll = "select count(*) from GTMessage";
            //RowCount = Convert.ToInt32(DBHelper.ExecuteScalar(sqll));
            DataTable dt = DBHelper.GetTable(sql);

            List<GModel> list = new List<GModel>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                GModel g = new GModel();

                g.GId = Convert.ToInt32(dt.Rows[i]["GId"]);
                g.GCarId = dt.Rows[i]["GCarId"].ToString();
                g.GHomeId = dt.Rows[i]["GHomeId"].ToString();
                g.GGoods = dt.Rows[i]["GGoods"].ToString();
                g.GTime = dt.Rows[i]["GTime"].ToString();
                g.GName = dt.Rows[i]["GName"].ToString();
                list.Add(g);
            }

            return list;
        }

Controllers

public  ActionResult SearchistByPager(string GGoods,string GName)
        {
            int PageIndex = 1;
            int PageSize = 5;
            int RowCount = 0;
            int PageCount = 0;
            if (Request["PageIndex"] != null)
            {
                PageIndex = Convert.ToInt32(Request["PageIndex"]);
            }

            if (PageIndex < 1)
            {
                PageIndex = 1;
            }
            if (Session["PageCount"] != null && PageIndex > Convert.ToInt32(Session["PageCount"]))
            {
                PageIndex = Convert.ToInt32(Session["PageCount"]);
            }


            ViewData["PIndex"] = PageIndex;
            //List<GModel> list = bll.GetListByPager(PageIndex, PageSize, out RowCount);

            PageCount = RowCount % PageSize == 0 ? RowCount / PageSize : RowCount / PageSize + 1;
            Session["PageCount"] = PageCount;
            return View("Index",bll.SearchistByPager(PageIndex,PageSize,GGoods,GTime));
        }

 

 


 


 


 

 

对于动态下拉框的数据获取,绑定如下

首先需要在DAL层查询主表数据

public List<RTypeModel> GetRTList()
        {
            string sql = "select RTid,RTname from RType  ";
            DataTable dt = DBHelper.GetTable(sql);
            List<RTypeModel> list = new List<RTypeModel>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RTypeModel rt = new RTypeModel();
                rt.RTid = Convert.ToInt32(dt.Rows[i]["RTid"]);
                rt.RTname = dt.Rows[i]["RTname"].ToString();
                list.Add(rt);
            }

            return list;
        }

在Controllers中需要使用ViewBag存值向视图传值

ViewBag.RTid = new SelectList(bll.GetRTList(), "RTid", "RTname");

在视图中绑定下拉框数据

@Html.DropDownList("RTid")

posted @ 2017-06-30 10:36  家宁  阅读(261)  评论(0编辑  收藏  举报