asp.net操作sql server数据库(AspNetSqlServer实现数据的增删改查)

首先看数据库的代码:

-- 创建数据库 create database musicdb use musicdb -- 创建表 -- 用户表 create table [user]( uid int primary key identity(1,1), name varchar(20) not null, pwd varchar(20) not null, [desc] varchar(50) ) create table music( mid int primary key identity(101,1), musicName varchar(50) not null, singer varchar(20) not null, --歌手 album varchar(20) not null, --专辑 timer varchar(20), --时长 position varchar(100), --位置 ) select * from music select * from [user]

然后vs2019 创建AspNet Web项目,添加Default.aspx

有一定基础的同学,应该知道,AspNet 分两部分,一部分是前台界面,另一部分是后台代码,Aspnet使用了CodeBehind 代码后置的技术

Default.aspx 前台设计界面:

<div class="row"> <h3 class="text-center" style="color: orange">音乐库 管理</h3> </div> <div class="row"> <asp:GridView ID="GridView1" runat="server" CssClass="table table-hover table-striped" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" DataKeyNames="mid" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"> <Columns> <asp:BoundField DataField="mid" HeaderText="序号" /> <asp:BoundField DataField="musicName" HeaderText="歌曲名" /> <asp:BoundField DataField="singer" HeaderText="歌手" /> <asp:BoundField DataField="album" HeaderText="专辑" /> <asp:BoundField DataField="timer" HeaderText="时长" /> <asp:BoundField DataField="position" HeaderText="存储位置" /> <asp:TemplateField HeaderText="操作" ShowHeader="False"> <EditItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="更新"></asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton> </EditItemTemplate> <ItemTemplate> <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Edit" Text="修改"></asp:LinkButton> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="删除"></asp:LinkButton> <a href="/add.aspx">插入</a> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div>

Default.aspx.cs 后台代码

public partial class _Default : Page { public string name = ""; protected void Page_Load(object sender, EventArgs e) // 首次加载 { if (!IsPostBack) { if (Session["userName"]==null) { Response.Redirect("~/login.aspx"); } else { name = Session["userName"].ToString(); LoadMusic(); } } } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string id = GridView1.Rows[e.RowIndex].Cells[0].Text; string sql = $"delete from music where mid='{id}'"; if (DBHelper.Update(sql)) { LoadMusic(); } else { Response.Write("<script>alert('删除失败!')</script>"); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { //string mid = e.CommandArgument.ToString(); name = Session["userName"].ToString(); GridView1.EditIndex = e.NewEditIndex; LoadMusic(); } private void LoadMusic() { this.GridView1.DataSource = DBHelper.Select("select * from music"); this.GridView1.DataBind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString(); string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text; string signer= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text; string album= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text; string timer= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text; string p= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text; string sql = $"update music set musicName='{name}',singer='{signer}',album='{album}',timer='{timer}',position='{p}' where mid='{id}'"; if (DBHelper.Update(sql)) { Response.Write("<script>alert('修改成功!')</script>"); LoadMusic(); } } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { name = Session["userName"].ToString(); GridView1.EditIndex = -1; LoadMusic(); } }

效果图

asp.net操作sql server数据库(AspNetSqlServer实现数据的增删改查)(1)

以上就是首页完成的显示的功能,篇幅有限,需要源码的朋友私聊我噢

asp.net操作sql server数据库(AspNetSqlServer实现数据的增删改查)(2)

千山万水总是情,打赏一元也是情

和老段一起终身学习,我们一起做自己的CEO!

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页