三層架構下GridView控件實現增刪改查
轉自:https://blog.csdn.net/iteye_3224/article/details/82373073
第一步:建立三層,並添加他們之間的引用關系,如下圖所示:
第二步:添加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>");
-
}
-
}
-
}