"Meticulous step by step" - GridView control realizes addition, deletion, modification and inspection under the three-tier architecture

Step 1: Establish three layers and add a reference relationship between them, as shown in the following figure:

Step 2 : Add a GridView table and apply the formatting style, as shown in the following figure:


Step 3 : Click the small triangle on the right side of the table and select Edit Column, as shown in the following figure:


Step 4 : Add three binding columns and bind data to them, as shown in the following figure:




And modify the name of the command line to "Management", as shown below:


Below is the overall architecture of the source code


Next is all the source code:

Users of the DiaryModels layer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiaryModels
{
    /// <summary>
    /// 用户类
    /// </summary>
    public class Users
    {
        private int _UserID;
        private string _UserName;
        private string _password;
        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserID {
            get { return _UserID; }
            set { _UserID = value; }
        }
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName {
            get { return _UserName; }
            set { _UserName = value; }
        }
        /// <summary>
        /// 密码
        /// </summary>
        public string password {
            get { return _password; }
            set { _password = value; }
        }
    }
}

UserDAO of DiaryDAL layer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using DiaryModels;
using System.Data;
using System.Data.SqlClient;

namespace DiaryDAL
{
    public class UserDAO
    {
        Helper help = new Helper();
        /// <summary>
        /// 添加一条用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Add(Users user) {
            string cmdText = "insert into Users(UserID,UserName,password) values(@UserID,@UserName,@password)";
            SqlParameter[] param = new SqlParameter[3];
            param[0]=new SqlParameter("@UserID",user.UserID);
            param[1] = new SqlParameter("@UserName", user.UserName);
            param[2] = new SqlParameter("@password", user.password);
            return help.ExecMake(cmdText, CommandType.Text, param);
        }
        /// <summary>
        /// 删除一条用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Delete(Users user) {
            string cmdText = "delete from Users where UserID=@UserID";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@UserID", user.UserID);
            return help.ExecMake(cmdText, CommandType.Text, param);
        }
        /// <summary>
        /// 修改一条用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Modify(Users user) {
            string cmdText = "update Users set UserName =@UserName,password =@password where UserID =@UserID";
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@UserName", user.UserName);
            param[1] = new SqlParameter("@password", user.password);
            param[2] = new SqlParameter("@UserID", user.UserID);
            return help.ExecMake(cmdText, CommandType.Text, param);
        }
        public DataTable Select() {
            string cmdText = "select * from Users";
            return help.ExecSelect(cmdText, CommandType.Text);
        }
    }
}

Helper class of DiaryDAL layer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DiaryDAL
{
    public class Helper
    {
        /// <summary>
        /// 执行带参数的增删改语句
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="Paras"></param>
        /// <returns></returns>
        public bool ExecMake(string cmdText, CommandType Cmdtype, SqlParameter[] Paras)
        {
           
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
            con.Open();
            //对SqlCommand对象进行初始化
            SqlCommand SqlComm = new SqlCommand(cmdText, con);
            SqlComm.CommandType = Cmdtype;
            //对参数赋值
            SqlComm.Parameters.AddRange(Paras);
            //执行命令
            SqlComm.ExecuteNonQuery();
            con.Close();
            return true;

        }
        
        /// <summary>
        /// 执行不带参数的查询语句
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="Paras"></param>
        /// <returns></returns>
        public DataTable ExecSelect(string cmdText, CommandType Cmdtype)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
            con.Open();

            //对SqlCommand对象进行实例化
            SqlCommand SqlComm = new SqlCommand(cmdText, con);
            SqlComm.CommandType = Cmdtype;
            //给参数赋值
            SqlComm.CommandTimeout = 10000;

            //返回DataReader对象
            SqlDataReader DataReader = SqlComm.ExecuteReader();
            DataTable Dt = new DataTable();
            //返回DataTable对象
            Dt.Load(DataReader);

            //返回数据表
            con.Close();
            return Dt;
        }
        
    }
}

The UserManager of the business logic layer DiaryBLL does not judge, the reader can understand the meaning

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DiaryModels;
using DiaryDAL;

namespace DiaryBLL
{
    public class UserManager
    {
        UserDAO userdao = new UserDAO();
        public bool Add(Users user) {
            return userdao.Add(user);
        }
        public bool Delete(Users user) {
            return userdao.Delete(user);
        }
        public bool Modify(Users user) {
            return userdao.Modify(user);
        }
        public DataTable Select() {
            return userdao.Select();
        }
    }
}
Presentation layer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiaryModels;
using DiaryBLL;

public partial class _Default : System.Web.UI.Page
{
    UserManager usermgr = new UserManager();
    Users user = new Users();
    
    //为表格绑定数据源
    private void Bind() {
        GridView1.DataSource = usermgr.Select();
        GridView1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            Bind();
        }
    }

    //添加用户信息
    protected void btnOk_Click(object sender, EventArgs e)
    {
        user.UserID = Convert.ToInt16( txtUserID.Text);
        user.UserName = txtUserName.Text;
        user.password = txtPassword.Text;
        if (usermgr.Add(user))
        {
            Response.Write("<script>('添加成功!')</script>");
            Bind();
        }
        else {
            Response.Write("<script>('添加失败!')</script>");
        }
        
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        user.UserID = Convert.ToInt16(((GridView1.Rows[e.RowIndex].FindControl("Label1") as Label).Text));
        if (usermgr.Delete(user))
        {
            Response.Write("<script>('删除成功!')</script>");
            Bind();
        }
        else {
            Response.Write("<script>('删除失败!')</script>");
        }
    }
//让当前行处于修改状态
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bind();
    }
    //取消编辑
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bind();
    }
    //更新至数据库
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        user.UserID = Convert.ToInt16((GridView1.Rows[e.RowIndex].FindControl("Label1")as Label).Text);
        user.UserName = (GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text;
        user.password = (GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text;

        if (usermgr.Modify(user))
        {
            Response.Write("<script>('修改成功!')</script>");
            GridView1.EditIndex = -1;
            Bind();
        }
        else {
            Response.Write("<script>('修改失败!')</script>");
        }
    }
}







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324136970&siteId=291194637