第一步:建立三层,并添加他们之间的引用关系,如下图所示:
第二步:添加GridView表格,并且套用格式样式,如下图所示:
第三步:点击表格右侧的小三角,并选中编辑列,如下图所示:
第四步:添加三个绑定列,并为其绑定上数据,如下图所示:
并且修改命令列的名称为“管理”,做好之后如下图所示:
下面是源代码的整体架构
接下来是所有的源代码:
DiaryModels层的Users
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; } } } }
DiaryDAL层的UserDAO
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); } } }
DiaryDAL层的Helper类
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; } } }
业务逻辑层DiaryBLL的UserManager没有进行判断,读者明白意思即可
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(); } } }表示层:
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>"); } } }
- 3楼lmdcszh昨天 08:02
- 又见三层啊
- 2楼lfmilaoshi前天 19:30
- 文尾,要是有个总结陈词,就是画龙点睛了。。。米老师
- Re: wangboxian昨天 22:21
- 回复lfmilaoshin嗯,下次注意,米老师
- 1楼lilongsheng11253天前 16:21
- 很好,学习
- Re: wangboxian前天 19:15
- 回复lilongsheng1125n常来啊