程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能

发布于2023-01-18 22:41     阅读(423)     评论(0)     点赞(28)     收藏(5)


ASP.NET Web——GridView

完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能


环境说明

系统要求:win7/10/11

开发语言:C#

开发工具:Visual Studio 2012/2017/2019/2022,本示例使用的是Visual Studio 2017

项目创建:ASP.NET Web应用程序(.NET Framework)

数据库:SQLServer 2012/2014/2017/2019,本示例使用的是SQLServer 2014

数据库工具:Navicat

功能演示

ASP.NET Web增删改查演示(ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能)

数据库脚本

建表语句

  1. CREATE TABLE [dbo].[users] (
  2. [id] varchar(32) COLLATE Chinese_PRC_CI_AS NOT NULL DEFAULT (replace(newid(),'-','')) ,
  3. [createDate] datetime NOT NULL DEFAULT (getdate()) ,
  4. [userName] varchar(20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
  5. [age] int NOT NULL ,
  6. [introduce] varchar(200) COLLATE Chinese_PRC_CI_AS NOT NULL ,
  7. CONSTRAINT [PK__users__3213E83F0E2177B8] PRIMARY KEY ([id])
  8. )
  9. ON [PRIMARY]
  10. GO

信息插入

  1. INSERT INTO [dbo].[users] ([id], [createDate], [userName], [age], [introduce]) VALUES (N'1a19c0945dfc44e98a715ffccdb1cc54', N'2223-08-08 18:18:22.000', N'superGirl777', N'17', N'超级女孩');
  2. GO
  3. INSERT INTO [dbo].[users] ([id], [createDate], [userName], [age], [introduce]) VALUES (N'1EBB75FB1DD64B678413894A4B736484', N'2222-08-08 18:18:22.000', N'貂蝉', N'16', N'吕布爱妻');
  4. GO
  5. INSERT INTO [dbo].[users] ([id], [createDate], [userName], [age], [introduce]) VALUES (N'7979e6d162c44ccbaf47fd3b0172ecf3', N'2222-01-01 01:01:01.000', N'周瑜', N'32', N'大都督');
  6. GO
  7. INSERT INTO [dbo].[users] ([id], [createDate], [userName], [age], [introduce]) VALUES (N'9BFE04E8999F415D9224CCFCEEF40927', N'2222-08-08 18:18:22.000', N'赵子龙', N'27', N'子龙浑身都是胆');
  8. GO

创建ASP.NET Web项目

选择左侧菜单栏中的【Web】项目,右侧会弹出对应的ASP.NET Web应用程序(.NET Framework)

选择创建【Web窗体】

创建三层关系

创建类库并完成三层关系

三层关系

引入方式

注意层级引入顺序

完成DAL层DBHelper

注意换成自己的数据库连接

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Data.SqlClient;
  8. namespace DAL
  9. {
  10. public class DBHelper
  11. {
  12. //数据库连接地址
  13. private static string url = "Data Source=.;Initial Catalog=test;Integrated Security=True";
  14. public static DataTable Query(string sql)
  15. {
  16. SqlConnection conn = new SqlConnection(url);//创建链接
  17. SqlDataAdapter sdap = new SqlDataAdapter(sql,conn);//闭合式查询
  18. DataSet ds = new DataSet();//结果集
  19. sdap.Fill(ds);//将闭合式查询的结果放置到结果集中
  20. return ds.Tables[0];//返回结果集中的第一项
  21. }
  22. public static bool NoQuery(string sql) {
  23. SqlConnection conn = new SqlConnection(url);//创建链接
  24. conn.Open();//打开数据库连接
  25. SqlCommand cmd = new SqlCommand(sql,conn);//声明操作
  26. int rows=cmd.ExecuteNonQuery();//执行操作
  27. conn.Close();//关闭数据库连接
  28. return rows > 0;//判断是否操作成功
  29. }
  30. }
  31. }

完成DAL层UsersDAL.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace DAL
  8. {
  9. public class UsersDAL
  10. {
  11. /// <summary>
  12. /// 查询所有
  13. /// </summary>
  14. /// <returns></returns>
  15. public static DataTable GetAll() {
  16. string sql = "select * from users";
  17. return DBHelper.Query(sql);
  18. }
  19. /// <summary>
  20. /// 模糊查询
  21. /// </summary>
  22. /// <param name="userName"></param>
  23. /// <returns></returns>
  24. public static DataTable GetSelectByName(string userName)
  25. {
  26. string sql = "select * from users where userName like '%" + userName + "%'";
  27. return DBHelper.Query(sql);
  28. }
  29. /// <summary>
  30. /// 添加操作DAL
  31. /// </summary>
  32. /// <param name="userName"></param>
  33. /// <param name="age"></param>
  34. /// <param name="introduce"></param>
  35. /// <returns></returns>
  36. public static bool AddInfo(string userName, int age, string introduce) {
  37. string id = Guid.NewGuid().ToString("N");
  38. string createDate = "2023-1-4 15:24:15";
  39. string sql = string.Format("insert into users values('{0}','{1}','{2}',{3},'{4}')",
  40. id, createDate, userName, age, introduce);
  41. return DBHelper.NoQuery(sql);
  42. }
  43. /// <summary>
  44. /// 删除语句
  45. /// </summary>
  46. /// <param name="id"></param>
  47. /// <returns></returns>
  48. public static bool DeleteById(string id) {
  49. string sql = "delete from users where id='" + id + "'";
  50. return DBHelper.NoQuery(sql);
  51. }
  52. /// <summary>
  53. /// 根据id进行精准查询
  54. /// </summary>
  55. /// <param name="id"></param>
  56. /// <returns></returns>
  57. public static DataTable GetSelectById(string id)
  58. {
  59. string sql = "select * from users where id='"+id+"'";
  60. return DBHelper.Query(sql);
  61. }
  62. /// <summary>
  63. /// 修改
  64. /// </summary>
  65. /// <param name="id"></param>
  66. /// <param name="age"></param>
  67. /// <param name="introduce"></param>
  68. /// <returns></returns>
  69. public static bool UpdateById(string id,string age,string introduce) {
  70. string sql = string.Format("update users set age={0},introduce='{1}' where id='{2}'",
  71. age,introduce,id);
  72. return DBHelper.NoQuery(sql);
  73. }
  74. }
  75. }

完成BLL层UsersBLL.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BLL
  8. {
  9. public class UsersBLL
  10. {
  11. public static DataTable GetAll()
  12. {
  13. return DAL.UsersDAL.GetAll();
  14. }
  15. public static DataTable GetSelectByName(string userName)
  16. {
  17. return DAL.UsersDAL.GetSelectByName(userName);
  18. }
  19. public static bool AddInfo(string userName, int age, string introduce)
  20. {
  21. return DAL.UsersDAL.AddInfo(userName, age, introduce);
  22. }
  23. public static bool DeleteById(string id)
  24. {
  25. return DAL.UsersDAL.DeleteById(id);
  26. }
  27. public static DataTable GetSelectById(string id)
  28. {
  29. return DAL.UsersDAL.GetSelectById(id);
  30. }
  31. public static bool UpdateById(string id, string age, string introduce)
  32. {
  33. return DAL.UsersDAL.UpdateById(id,age,introduce);
  34. }
  35. }
  36. }

完成视图层Index.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Demo8.Index" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <title></title>
  7. <link href="Content/bootstrap.css" rel="stylesheet" />
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12. <p>
  13. <asp:TextBox runat="server" ID="selectKey"></asp:TextBox>
  14. <asp:Button runat="server" Text="查询" OnClick="Unnamed_Click"/>
  15. </p>
  16. <a href="AddInfo.aspx" class="btn btn-primary">添加数据</a>
  17. <asp:GridView CssClass="table table-bordered table-hover" runat="server" ID="showList" AutoGenerateColumns="false" OnRowCommand="showList_RowCommand">
  18. <Columns>
  19. <asp:BoundField DataField="id" HeaderText="编号"/>
  20. <asp:BoundField DataField="createDate" HeaderText="创建时间"/>
  21. <asp:BoundField DataField="userName" HeaderText="昵称"/>
  22. <asp:BoundField DataField="age" HeaderText="年龄"/>
  23. <asp:BoundField DataField="introduce" HeaderText="简介"/>
  24. <asp:TemplateField>
  25. <ItemTemplate>
  26. <asp:LinkButton runat="server" CommandName="UpdateById" CommandArgument='<%# Eval("id") %>'
  27. CssClass="btn btn-primary">修改</asp:LinkButton>
  28. <asp:LinkButton runat="server" CommandName="DeleteById" CommandArgument='<%# Eval("id") %>'
  29. OnClientClick="return confirm('是否删除此行?')" CssClass="btn btn-primary">删除</asp:LinkButton>
  30. </ItemTemplate>
  31. </asp:TemplateField>
  32. </Columns>
  33. </asp:GridView>
  34. </div>
  35. </form>
  36. </body>
  37. </html>

完成后台Index.aspx.cs

  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. namespace Demo8
  8. {
  9. public partial class Index : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!IsPostBack)
  14. {
  15. //绑定数据
  16. this.showList.DataSource = BLL.UsersBLL.GetAll();
  17. //显示数据
  18. this.showList.DataBind();
  19. }
  20. }
  21. protected void Unnamed_Click(object sender, EventArgs e)
  22. {
  23. string selectKey = this.selectKey.Text;
  24. this.showList.DataSource = BLL.UsersBLL.GetSelectByName(selectKey);
  25. //显示数据
  26. this.showList.DataBind();
  27. }
  28. protected void showList_RowCommand(object sender, GridViewCommandEventArgs e)
  29. {
  30. if (e.CommandName == "DeleteById")
  31. {
  32. BLL.UsersBLL.DeleteById(e.CommandArgument.ToString());
  33. Response.Redirect("Index.aspx");
  34. } else if (e.CommandName == "UpdateById") {
  35. Response.Redirect("UpdateById.aspx?id="+e.CommandArgument.ToString());
  36. }
  37. }
  38. }
  39. }

完成视图层AddInfo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddInfo.aspx.cs" Inherits="Demo8.AddInfo" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title></title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <p>
  12. <asp:TextBox runat="server" ID="userName" placeholder="请输入昵称"></asp:TextBox>
  13. </p>
  14. <p>
  15. <asp:TextBox runat="server" ID="age" placeholder="请输入年龄"></asp:TextBox>
  16. </p>
  17. <p>
  18. <asp:TextBox runat="server" ID="introduce" placeholder="请输入简介"></asp:TextBox>
  19. </p>
  20. <p>
  21. <asp:Button runat="server" Text="添加" OnClick="Unnamed_Click"/>
  22. </p>
  23. </div>
  24. </form>
  25. </body>
  26. </html>

完成后台AddInfo.aspx.cs

  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. namespace Demo8
  8. {
  9. public partial class AddInfo : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void Unnamed_Click(object sender, EventArgs e)
  15. {
  16. string userName=this.userName.Text;
  17. int age=int.Parse(this.age.Text);
  18. string introduce = this.introduce.Text;
  19. if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(this.age.Text)
  20. || string.IsNullOrEmpty(introduce)) {
  21. Response.Write("<script>alert('参数不允许有空存在!');</script>");
  22. return;
  23. }
  24. bool isf=BLL.UsersBLL.AddInfo(userName,age,introduce);
  25. if (isf)
  26. {
  27. Response.Redirect("Index.aspx");
  28. }
  29. else {
  30. Response.Write("<script>alert('添加失败!');</script>");
  31. }
  32. }
  33. }
  34. }

完成视图层UpdateById.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpdateById.aspx.cs" Inherits="Demo8.UpdateById" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title></title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <p>
  12. <asp:TextBox runat="server" ID="id" ReadOnly="true"></asp:TextBox>
  13. </p>
  14. <p>
  15. <asp:TextBox runat="server" ID="age"></asp:TextBox>
  16. </p>
  17. <p>
  18. <asp:TextBox runat="server" ID="introduce"></asp:TextBox>
  19. </p>
  20. <p>
  21. <asp:Button runat="server" Text="提交修改" OnClick="Unnamed_Click"/>
  22. </p>
  23. </div>
  24. </form>
  25. </body>
  26. </html>

完成后台UpdateById.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. namespace Demo8
  9. {
  10. public partial class UpdateById : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if (!IsPostBack) {
  15. string id = Request.QueryString["id"];
  16. DataTable dt = BLL.UsersBLL.GetSelectById(id);
  17. this.id.Text = dt.Rows[0]["id"].ToString();
  18. this.age.Text = dt.Rows[0]["age"].ToString();
  19. this.introduce.Text = dt.Rows[0]["introduce"].ToString();
  20. }
  21. }
  22. protected void Unnamed_Click(object sender, EventArgs e)
  23. {
  24. string id=this.id.Text;
  25. string age = this.age.Text;
  26. string introduce = this.introduce.Text;
  27. if (
  28. string.IsNullOrEmpty(id)||
  29. string.IsNullOrEmpty(age)||
  30. string.IsNullOrEmpty(introduce)
  31. ) {
  32. Response.Write("<script>alert('参数不允许为空!');<script>");
  33. return;
  34. }
  35. BLL.UsersBLL.UpdateById(id,age,introduce);
  36. Response.Redirect("Index.aspx");
  37. }
  38. }
  39. }

最终执行效果:

项目源码地址:

ASP.NETWeb-GridView完整增删改查示例项目源码-大二结业考试必备技能-C#文档类资源-CSDN下载

原文链接:https://blog.csdn.net/feng8403000/article/details/128576341




所属网站分类: 技术文章 > 博客

作者:听说你混的不错

链接:http://www.qianduanheidong.com/blog/article/494755/d918b07b67cdb729cae6/

来源:前端黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

28 0
收藏该文
已收藏

评论内容:(最多支持255个字符)