如果是是学习的介意别直接“复制”“粘贴”,代码看懂了跟会写完全是两码事(貌似我也是菜鸟,挖哈哈)
1.Model数据模型下的admin.cs类中的代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace Model
{
public class admin
{
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _adminname;
public string Adminname
{
get { return _adminname; }
set { _adminname = value; }
}
private int _adminpwd;
public int Adminpwd
{
get { return _adminpwd; }
set { _adminpwd = value; }
}
}
}
2.DAL层下的admin.cs类中的代码:
using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DAL
{
public class admin
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["add_usersConn"].ConnectionString);
public DataSet GetAllRow()
{
string str = "select id,adminname,adminpwd from admin";
SqlDataAdapter da = new SqlDataAdapter(str,sqlcon);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public bool update(Model.admin model)
{
string str = "update admin set adminname = @adminname,adminpwd = @adminpwd where id = @id";
sqlcon.Open();
SqlCommand com = new SqlCommand(str,sqlcon);
com.Parameters.AddWithValue("@adminname", model.Adminname);
com.Parameters.AddWithValue("@adminpwd", model.Adminpwd);
com.Parameters.AddWithValue("@id", model.Id);
int flag = com.ExecuteNonQuery();
sqlcon.Close();
if (flag > 0)
{
return true;
}
else
{
return false;
}
}
public bool del(Model.admin model)
{
string str = "delete from admin where id=@id";
sqlcon.Open();
SqlCommand com = new SqlCommand(str,sqlcon);
com.Parameters.AddWithValue("@id",model.Id);
int flag = com.ExecuteNonQuery();
sqlcon.Close();
if (flag > 0)
{
return true;
}
else
{
return false;
}
}
public bool insert(Model.admin model)
{
string str = "insert into admin(adminname,adminpwd) values(@adminname,@adminpwd)";
sqlcon.Open();
SqlCommand com = new SqlCommand(str,sqlcon);
com.Parameters.AddWithValue("@adminname",model.Adminname);
com.Parameters.AddWithValue("@adminpwd",model.Adminpwd);
int flag = com.ExecuteNonQuery();
sqlcon.Close();
if (flag > 0)
{
return true;
}
else
{
return false;
}
}
}
}
3.BLL层中admin.cs
using System;
using System.Collections.Generic;
using System.Text;
using Model;
using DAL;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace BLL
{
public class admin
{
Model.admin model = new Model.admin();
DAL.admin dal = new DAL.admin();
public DataSet GetAllRow()
{
return dal.GetAllRow();
}
public bool update(Model.admin model)
{
return dal.update(model);
}
public bool del(Model.admin model)
{
return dal.del(model);
}
public bool insert(Model.admin model)
{
return dal.insert(model);
}
}
}
4.UI层,即Web层下的后代文件Default.aspx.cs代码(赠送一个DataList分页,挖哈哈):
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Model;
using BLL;
public partial class _Default : System.Web.UI.Page
{
Model.admin model = new Model.admin();
BLL.admin bll = new BLL.admin();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
page_bind();
}
}
public void bind()
{
DataSet ds = bll.GetAllRow();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
}
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)
{
string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
string pwd = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
model.Id = Convert.ToInt32(id);
model.Adminpwd = Convert.ToInt32(pwd);
model.Adminname = name;
bool flag = bll.update(model);
if (flag == true)
{
Response.Write("<script>alert('修改成功!');</script>");
GridView1.EditIndex = -1;
bind();
}
else
{
Response.Write("<script>alert('修改失败!');</script>");
bind();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
model.Id = id;
bool flag = bll.del(model);
if (flag == true)
{
Response.Write("<script>alert('删除成功!');</script>");
bind();
}
else
{
Response.Write("<script>alert(删除失败!');</script>");
bind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
model.Adminname = Txtname.Text.ToString();
model.Adminpwd = Convert.ToInt32(Txtpwd.Text.ToString());
bool flag = bll.insert(model);
if (flag == true)
{
Response.Write("<script>alert('添加成功!');</script>");
bind();
}
else
{
Response.Write("<script>alert('添加失败!');</script>");
bind();
}
}
public void page_bind()
{
int curpage = Convert.ToInt32(Lbcurpage.Text.ToString());
PagedDataSource ps = new PagedDataSource();
DataSet ds = bll.GetAllRow();
ps.DataSource = ds.Tables[0].DefaultView;
ps.AllowPaging=true;
ps.PageSize = 5;
ps.CurrentPageIndex = curpage - 1;
Lbpagecount.Text = ps.PageCount.ToString();
LinkBtnFirst.Enabled = true;
LinkBtnPrev.Enabled = true;
LinkBtnNext.Enabled = true;
LinkBtnLast.Enabled = true;
if (curpage == 1)
{
LinkBtnFirst.Enabled = false;
LinkBtnPrev.Enabled = false;
}
if (curpage == ps.PageCount)
{
LinkBtnNext.Enabled = false;
LinkBtnLast.Enabled = false;
}
DataList1.DataSource = ps;
DataList1.DataKeyField = "id";
DataList1.DataBind();
}
protected void LinkBtnFirst_Click(object sender, EventArgs e)
{
Lbcurpage.Text = "1";
page_bind();
}
protected void LinkBtnPrev_Click(object sender, EventArgs e)
{
Lbcurpage.Text = Convert.ToString(Convert.ToInt32(Lbcurpage.Text)-1);
page_bind();
}
protected void LinkBtnNext_Click(object sender, EventArgs e)
{
Lbcurpage.Text = Convert.ToString(Convert.ToInt32(Lbcurpage.Text) + 1);
page_bind();
}
protected void LinkBtnLast_Click(object sender, EventArgs e)
{
Lbcurpage.Text = Lbpagecount.Text;
page_bind();
}
}
5.UI层,即Web层下的前台文件Default.aspx代码(赠送一个DataList分页,挖哈哈):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form >末页</asp:LinkButton>
</div>
</form>
</body>
</html>
6.额,别忘了在配置文件web.config中添加一个名为add_usersConn的数据库链接
例如:
<connectionStrings>
<add name="add_usersConn" connectionString="Data Source=PC-200906201516;Initial Catalog=add_users;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
附录:
数据库名:add_users
表名:admin
字段名 | 数据类型
id | int
adminname | nvarchar(50)
adminpwd | int