LINQ 基本增删改
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style type="text/css"> #student { width:100%; font-family:微软雅黑; font-size:20px; text-align:center; background-color:blue; } #tr_head { color:white; } .tr_main { background-color:white; } </style> </head> <body> <form id="form1" runat="server"> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table id="student"> <tr id="tr_head"> <td>Code</td> <td>Name</td> <td>Sex</td> <td>Birthday</td> <td>Class</td> <td>Score</td> <td>操作</td> </tr> </HeaderTemplate> <ItemTemplate> <tr class="tr_main"> <td><%#Eval("Code") %></td> <td><%#Eval("Name") %></td> <td><%#Eval("Sex") %></td> <td><%#Eval("Birthday","{0:yyyy年MM月dd日}") %></td> <td><%#Eval("Class") %></td> <td><%#Eval("Score") %></td> <td> <a href="delete.aspx?code=<%#Eval("Code") %>" >删除</a> <a href="update.aspx?code=<%#Eval("Code") %>" >修改</a> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Repeater1.DataSource = new studentData().SelectAll(); Repeater1.DataBind(); } }
封装方法
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// studentData 的摘要说明 /// </summary> public class studentData { data0425classesDataContext conn = null;//初始化LINQ类 public studentData() { conn = new data0425classesDataContext();//实例化LINQ类 } //查询所有信息 public List<student> SelectAll() { return conn.student.ToList(); } //插入信息 public void Insert( student ss) { conn.student.InsertOnSubmit(ss); conn.SubmitChanges(); } //删除信息 public void delete(int c) { var stu = conn.student.Where(r=>r.Code==c).FirstOrDefault();//兰姆达表达式查出该条信息的所有内容 conn.student.DeleteOnSubmit(stu);//删除该条信息 conn.SubmitChanges();//执行删除 } //查询一条信息 public student select(int c) { return conn.student.Where(r => r.Code == c).FirstOrDefault();//查询一条信息 } //改变信息 public void update(student s) { var stu = conn.student.Where(r => r.Code == s.Code).FirstOrDefault(); stu.Name = s.Name; stu.Sex = s.Sex; stu.Birthday = s.Birthday; stu.Class = s.Class; stu.Score = s.Score; conn.SubmitChanges(); } }
删除页面
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class delete : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int cc = Convert.ToInt32(Request["Code"]); new studentData().delete(cc); Response.Redirect("Default1.aspx"); } }
修改页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="update.aspx.cs" Inherits="update" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <h1>修改页面</h1><br /> 编号:<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /> 姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> 性别:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /> 生日:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /> 班级:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /> 分数:<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" Text="确定" /> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class update : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; if(!IsPostBack) { int c = Convert.ToInt32(Request["Code"]); student s=new studentData().select(c); Label1.Text = Request["Code"]; TextBox1.Text = s.Name; TextBox2.Text = s.Sex.ToString(); TextBox3.Text = s.Birthday.ToString(); TextBox4.Text = s.Class; TextBox5.Text = s.Score.ToString(); } } void Button1_Click(object sender, EventArgs e) { student s = new student(); s.Code = Convert.ToInt32(Label1.Text); s.Name = TextBox1.Text; s.Sex = Convert.ToBoolean(TextBox2.Text); s.Birthday = Convert.ToDateTime(TextBox3.Text); s.Class = TextBox4.Text; s.Score = Convert.ToDecimal(TextBox5.Text); new studentData().update(s); Response.Redirect("Default1.aspx"); } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步