三层架构下GridView控件实现增删改查

三层架构下GridView控件实现增删改查

转自:https://blog.csdn.net/iteye_3224/article/details/82373073

第一步:建立三层,并添加他们之间的引用关系,如下图所示:

 

第二步:添加GridView表格,并且套用格式样式,如下图所示:

 

第三步:点击表格右侧的小三角,并选中编辑列,如下图所示:


 

第四步:添加三个绑定列,并为其绑定上数据,如下图所示:

 

 

并且修改命令列的名称为“管理”,做好之后如下图所示:

 

下面是源代码的整体架构

接下来是所有的源代码:

DiaryModels层的Users

 

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
     
  6.  
    namespace DiaryModels
  7.  
    {
  8.  
    /// <summary>
  9.  
    /// 用户类
  10.  
    /// </summary>
  11.  
    public class Users
  12.  
    {
  13.  
    private int _UserID;
  14.  
    private string _UserName;
  15.  
    private string _password;
  16.  
    /// <summary>
  17.  
    /// 用户ID
  18.  
    /// </summary>
  19.  
    public int UserID {
  20.  
    get { return _UserID; }
  21.  
    set { _UserID = value; }
  22.  
    }
  23.  
    /// <summary>
  24.  
    /// 用户名
  25.  
    /// </summary>
  26.  
    public string UserName {
  27.  
    get { return _UserName; }
  28.  
    set { _UserName = value; }
  29.  
    }
  30.  
    /// <summary>
  31.  
    /// 密码
  32.  
    /// </summary>
  33.  
    public string password {
  34.  
    get { return _password; }
  35.  
    set { _password = value; }
  36.  
    }
  37.  
    }
  38.  
    }

DiaryDAL层的UserDAO

 

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
    using System.Configuration;
  6.  
    using DiaryModels;
  7.  
    using System.Data;
  8.  
    using System.Data.SqlClient;
  9.  
     
  10.  
    namespace DiaryDAL
  11.  
    {
  12.  
    public class UserDAO
  13.  
    {
  14.  
    Helper help = new Helper();
  15.  
    /// <summary>
  16.  
    /// 添加一条用户信息
  17.  
    /// </summary>
  18.  
    /// <param name="user"></param>
  19.  
    /// <returns></returns>
  20.  
    public bool Add(Users user) {
  21.  
    string cmdText = "insert into Users(UserID,UserName,password) values(@UserID,@UserName,@password)";
  22.  
    SqlParameter[] param = new SqlParameter[ 3];
  23.  
    param[ 0]= new SqlParameter( "@UserID",user.UserID);
  24.  
    param[ 1] = new SqlParameter( "@UserName", user.UserName);
  25.  
    param[ 2] = new SqlParameter( "@password", user.password);
  26.  
    return help.ExecMake(cmdText, CommandType.Text, param);
  27.  
    }
  28.  
    /// <summary>
  29.  
    /// 删除一条用户信息
  30.  
    /// </summary>
  31.  
    /// <param name="user"></param>
  32.  
    /// <returns></returns>
  33.  
    public bool Delete(Users user) {
  34.  
    string cmdText = "delete from Users where UserID=@UserID";
  35.  
    SqlParameter[] param = new SqlParameter[ 1];
  36.  
    param[ 0] = new SqlParameter( "@UserID", user.UserID);
  37.  
    return help.ExecMake(cmdText, CommandType.Text, param);
  38.  
    }
  39.  
    /// <summary>
  40.  
    /// 修改一条用户信息
  41.  
    /// </summary>
  42.  
    /// <param name="user"></param>
  43.  
    /// <returns></returns>
  44.  
    public bool Modify(Users user) {
  45.  
    string cmdText = "update Users set UserName =@UserName,password =@password where UserID =@UserID";
  46.  
    SqlParameter[] param = new SqlParameter[ 3];
  47.  
    param[ 0] = new SqlParameter( "@UserName", user.UserName);
  48.  
    param[ 1] = new SqlParameter( "@password", user.password);
  49.  
    param[ 2] = new SqlParameter( "@UserID", user.UserID);
  50.  
    return help.ExecMake(cmdText, CommandType.Text, param);
  51.  
    }
  52.  
    public DataTable Select() {
  53.  
    string cmdText = "select * from Users";
  54.  
    return help.ExecSelect(cmdText, CommandType.Text);
  55.  
    }
  56.  
    }
  57.  
    }

DiaryDAL层的Helper类

 

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
    using System.Data;
  6.  
    using System.Data.SqlClient;
  7.  
    using System.Configuration;
  8.  
     
  9.  
    namespace DiaryDAL
  10.  
    {
  11.  
    public class Helper
  12.  
    {
  13.  
    /// <summary>
  14.  
    /// 执行带参数的增删改语句
  15.  
    /// </summary>
  16.  
    /// <param name="cmdText"></param>
  17.  
    /// <param name="Paras"></param>
  18.  
    /// <returns></returns>
  19.  
    public bool ExecMake(string cmdText, CommandType Cmdtype, SqlParameter[] Paras)
  20.  
    {
  21.  
     
  22.  
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings[ "conStr"]);
  23.  
    con.Open();
  24.  
    //对SqlCommand对象进行初始化
  25.  
    SqlCommand SqlComm = new SqlCommand(cmdText, con);
  26.  
    SqlComm.CommandType = Cmdtype;
  27.  
    //对参数赋值
  28.  
    SqlComm.Parameters.AddRange(Paras);
  29.  
    //执行命令
  30.  
    SqlComm.ExecuteNonQuery();
  31.  
    con.Close();
  32.  
    return true;
  33.  
     
  34.  
    }
  35.  
     
  36.  
    /// <summary>
  37.  
    /// 执行不带参数的查询语句
  38.  
    /// </summary>
  39.  
    /// <param name="cmdText"></param>
  40.  
    /// <param name="Paras"></param>
  41.  
    /// <returns></returns>
  42.  
    public DataTable ExecSelect(string cmdText, CommandType Cmdtype)
  43.  
    {
  44.  
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings[ "conStr"]);
  45.  
    con.Open();
  46.  
     
  47.  
    //对SqlCommand对象进行实例化
  48.  
    SqlCommand SqlComm = new SqlCommand(cmdText, con);
  49.  
    SqlComm.CommandType = Cmdtype;
  50.  
    //给参数赋值
  51.  
    SqlComm.CommandTimeout = 10000;
  52.  
     
  53.  
    //返回DataReader对象
  54.  
    SqlDataReader DataReader = SqlComm.ExecuteReader();
  55.  
    DataTable Dt = new DataTable();
  56.  
    //返回DataTable对象
  57.  
    Dt.Load(DataReader);
  58.  
     
  59.  
    //返回数据表
  60.  
    con.Close();
  61.  
    return Dt;
  62.  
    }
  63.  
     
  64.  
    }
  65.  
    }

业务逻辑层DiaryBLL的UserManager没有进行判断,读者明白意思即可

 

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
    using System.Data;
  6.  
    using DiaryModels;
  7.  
    using DiaryDAL;
  8.  
     
  9.  
    namespace DiaryBLL
  10.  
    {
  11.  
    public class UserManager
  12.  
    {
  13.  
    UserDAO userdao = new UserDAO();
  14.  
    public bool Add(Users user) {
  15.  
    return userdao.Add(user);
  16.  
    }
  17.  
    public bool Delete(Users user) {
  18.  
    return userdao.Delete(user);
  19.  
    }
  20.  
    public bool Modify(Users user) {
  21.  
    return userdao.Modify(user);
  22.  
    }
  23.  
    public DataTable Select() {
  24.  
    return userdao.Select();
  25.  
    }
  26.  
    }
  27.  
    }
表示层:
  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Web;
  5.  
    using System.Web.UI;
  6.  
    using System.Web.UI.WebControls;
  7.  
    using DiaryModels;
  8.  
    using DiaryBLL;
  9.  
     
  10.  
    public partial class _ Default : System. Web. UI. Page
  11.  
    {
  12.  
    UserManager usermgr = new UserManager();
  13.  
    Users user = new Users();
  14.  
     
  15.  
    //为表格绑定数据源
  16.  
    private void Bind() {
  17.  
    GridView1.DataSource = usermgr.Select();
  18.  
    GridView1.DataBind();
  19.  
    }
  20.  
    protected void Page_Load(object sender, EventArgs e)
  21.  
    {
  22.  
    if (!IsPostBack) {
  23.  
    Bind();
  24.  
    }
  25.  
    }
  26.  
     
  27.  
    //添加用户信息
  28.  
    protected void btnOk_Click(object sender, EventArgs e)
  29.  
    {
  30.  
    user.UserID = Convert.ToInt16( txtUserID.Text);
  31.  
    user.UserName = txtUserName.Text;
  32.  
    user.password = txtPassword.Text;
  33.  
    if (usermgr.Add(user))
  34.  
    {
  35.  
    Response.Write( "<script>('添加成功!')</script>");
  36.  
    Bind();
  37.  
    }
  38.  
    else {
  39.  
    Response.Write( "<script>('添加失败!')</script>");
  40.  
    }
  41.  
     
  42.  
    }
  43.  
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  44.  
    {
  45.  
    user.UserID = Convert.ToInt16(((GridView1.Rows[e.RowIndex].FindControl( "Label1") as Label).Text));
  46.  
    if (usermgr.Delete(user))
  47.  
    {
  48.  
    Response.Write( "<script>('删除成功!')</script>");
  49.  
    Bind();
  50.  
    }
  51.  
    else {
  52.  
    Response.Write( "<script>('删除失败!')</script>");
  53.  
    }
  54.  
    }
  55.  
    //让当前行处于修改状态
  56.  
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  57.  
    {
  58.  
    GridView1.EditIndex = e.NewEditIndex;
  59.  
    Bind();
  60.  
    }
  61.  
    //取消编辑
  62.  
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  63.  
    {
  64.  
    GridView1.EditIndex = -1;
  65.  
    Bind();
  66.  
    }
  67.  
    //更新至数据库
  68.  
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  69.  
    {
  70.  
    user.UserID = Convert.ToInt16((GridView1.Rows[e.RowIndex].FindControl( "Label1") as Label).Text);
  71.  
    user.UserName = (GridView1.Rows[e.RowIndex].FindControl( "TextBox2") as TextBox).Text;
  72.  
    user.password = (GridView1.Rows[e.RowIndex].FindControl( "TextBox3") as TextBox).Text;
  73.  
     
  74.  
    if (usermgr.Modify(user))
  75.  
    {
  76.  
    Response.Write( "<script>('修改成功!')</script>");
  77.  
    GridView1.EditIndex = -1;
  78.  
    Bind();
  79.  
    }
  80.  
    else {
  81.  
    Response.Write( "<script>('修改失败!')</script>");
  82.  
    }
  83.  
    }
  84.  
    }

猜你喜欢

转载自www.cnblogs.com/pacexdong/p/12218314.html