效果图:
(一)连接Car表 添加一个数据库方法(myDB)
namespace 连接数据库_查表 { public class myDB { private MYDBDataContext context = new MYDBDataContext(); public List<Car> Select()//将表全部查询 { return context.Car.ToList(); } public Car Select1(string code)//挨个查询 { var query = context.Car.Where(p => p.Code == code); if (query.Count() > 0) { return query.First(); } return null; } public bool Update(Car c)//修改 { var query = context.Car.Where(p => p.Code== c.Code); if (query.Count()>0) { Car a = query.First(); a.Name = c.Name; a.Brand = c.Brand; context.SubmitChanges(); return true; } else { return false; } } public void Delete(string code)//删除 { var query = context.Car.Where(p => p.Code == code); if (query.Count() > 0) { Car stu = query.First(); context.Car.DeleteOnSubmit(stu); context.SubmitChanges(); } } public bool All(Car a)//增加 { try { context.Car.InsertOnSubmit(a); context.SubmitChanges(); return true; } catch { return false; } } } }
效果图:
(二) 添加一个WEb窗口(che.aspx)
using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace 连接数据库_查表 { public partial class che : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e)//把表里所有的东西都显示出来 { List<Car> list = new myDB().Select(); string s = "<table width=‘100%‘ border=1 ><tr><td>编号</td><td>名字</td><td>类型</td><td>删除</td><td>增加</td></tr>"; foreach (Car date in list) { s += "<tr><td>" + date.Brand + "</td><td><a href=‘Update.aspx?code=" + date.Code + "‘>" + date.Name + "</a></td><td>" + date.Brand + "</td><td><a href=‘delete.aspx?code=" + date.Code + "‘>删除</a></td><td><a href=‘All.aspx‘>增加</a></td></tr>"; } s += "</table>"; Literal1.Text = s; } } }
(三)添加一个增加的Web窗体(All.aspx)
<视图>
<div> <asp:Label ID="Label1" runat="server" Text="编号"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label2" runat="server" Text="姓名"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label3" runat="server" Text="类型"></asp:Label> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确认增加" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回主页" /> </div>
<代码>
namespace 连接数据库_查表 { public partial class All : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e)//增加 { Car a = new Car(); a.Code = TextBox1.Text; a.Name = TextBox2.Text; new myDB().All(a); Response.Redirect("che.aspx"); } protected void Button2_Click(object sender, EventArgs e)//返回 { Response.Redirect("che.aspx");//转到主页的界面 } } }
(四)删除(delete.aspx)
添加一个delete Webfrom窗口
namespace 连接数据库_查表 { public partial class delete : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { new myDB().Delete(Request["code"].ToString()); Response.Redirect("che.aspx"); } } }
效果图:
(五) 修改(update.aspx)
<视图>
<div> <asp:Label ID="Label1" runat="server" Text="编号"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label2" runat="server" Text="姓名"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label3" runat="server" Text="类型"></asp:Label> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确认修改" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回主页" /> </div>
<代码>
namespace 连接数据库_查表 { public partial class Update : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string s = Request["code"].ToString() ; Car a = new myDB().Select1(s); Session["car"] = a; TextBox1.Text = a.Code; TextBox2.Text = a.Name; TextBox3.Text = a.Brand; } } protected void Button1_Click(object sender, EventArgs e)//修改 { Car c = (Car)Session["car"]; c.Name = TextBox2.Text; c.Brand = TextBox3.Text; bool isok= new myDB().Update(c); if (isok) { Response.Write("<script> alert(‘修改成功‘)</script>"); Response.Redirect("che.aspx");//转到主页的界面 } else { Response.Write("<script> alert(‘修改失败‘)</script>"); } } protected void Button2_Click(object sender, EventArgs e)//返回主页 { Response.Redirect("che.aspx");//转到主页的界面 } } }
时间: 2024-10-01 07:55:11