DataManage
aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataManage.aspx.cs" Inherits="DataManage" %>
<!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>基于Entity Framework利用LINQ进行数据访问管理</title>
</head>
<body>
<form id="form1" runat="server">
<div>
分类Id:<asp:TextBox ID="txtCategoryId" runat="server" Width="250px">输入分类Id,只用于“修改”和“删除”</asp:TextBox>
<br />
<asp:Button ID="btnQueryAll" runat="server" Text="显示全部" Width="80px" OnClick="btnQueryAll_Click" />
<asp:Button ID="btnFuzzy" runat="server" Text="模糊查找" Width="80px" OnClick="btnFuzzy_Click" />
<asp:Button ID="btnGroup" runat="server" Text="分组" OnClick="btnGroup_Click"/>
<asp:Button ID="btnPolymerize" runat="server" Text="最大值" OnClick="btnPolymerize_Click"/>
<asp:Button ID="btnOrder" runat="server" Text="降序排列" OnClick="btnOrder_Click"/>
<br />
<br />
<asp:GridView ID="gvCategory" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryId">
<Columns>
<asp:BoundField DataField="CategoryId" HeaderText="CategoryId" ReadOnly="True" SortExpression="CategoryId" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Descn" HeaderText="Descn" SortExpression="Descn" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataManage.aspx.cs" Inherits="DataManage" %>
<!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>基于Entity Framework利用LINQ进行数据访问管理</title>
</head>
<body>
<form id="form1" runat="server">
<div>
分类Id:<asp:TextBox ID="txtCategoryId" runat="server" Width="250px">输入分类Id,只用于“修改”和“删除”</asp:TextBox>
<br />
<asp:Button ID="btnQueryAll" runat="server" Text="显示全部" Width="80px" OnClick="btnQueryAll_Click" />
<asp:Button ID="btnFuzzy" runat="server" Text="模糊查找" Width="80px" OnClick="btnFuzzy_Click" />
<asp:Button ID="btnInsert" runat="server" Text="插入" Width="50px" OnClick="btnInsert_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="修改" Width="50px" />
<asp:Button ID="btnDelete" runat="server" Text="删除" Width="50px" />
<br />
<br />
<asp:GridView ID="gvCategory" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryId">
<Columns>
<asp:BoundField DataField="CategoryId" HeaderText="CategoryId" ReadOnly="True" SortExpression="CategoryId" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Descn" HeaderText="Descn" SortExpression="Descn" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DataManage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
MyPetShopEntities db = new MyPetShopEntities();
/// <summary>
///自定义方法Bind(),用于在gvCategory中显示Category表的最新结果
/// </summary>
protected void Bind() //本行应自行输入
{
var results = db.Table.ToList();
gvCategory.DataSource = results;
gvCategory.DataBind();
}
protected void btnQueryAll_Click(object sender, EventArgs e)
{
Bind();
}
protected void btnFuzzy_Click(object sender, EventArgs e)
{
Response.Redirect("FuzzyQuery.aspx");
}
protected void btnOrder_Click(object sender, EventArgs e)
{
MyPetShopEntities db = new MyPetShopEntities();
var results = db.Table.OrderByDescending(p => p.Descn);
gvCategory.DataSource = results.ToList();
gvCategory.DataBind();
}
protected void btnGroup_Click(object sender, EventArgs e)
{
MyPetShopEntities db = new MyPetShopEntities();
var results = db.Table.GroupBy(p => p.Name);
foreach (var g in results)
{
if (g.Key == "hw")
{
var results2 = from r in g select r;
gvCategory.DataSource = results2.ToList();
gvCategory.DataBind();
}
}
}
protected void btnPolymerize_Click(object sender, EventArgs e)
{
MyPetShopEntities db = new MyPetShopEntities();
var results = db.Table.GroupBy(p => p.CategoryId).Select(g => new
{
Key = g.Key,
Count = g.Count(),
MaxDescn = g.Max(p => p.Descn)
});
gvCategory.DataSource = results.ToList();
gvCategory.DataBind();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DataManage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
MyPetShopEntities db = new MyPetShopEntities();
/// <summary>
///自定义方法Bind(),用于在gvCategory中显示Category表的最新结果
/// </summary>
protected void Bind() //本行应自行输入
{
var results = db.Table.ToList();
gvCategory.DataSource = results;
gvCategory.DataBind();
}
protected void btnQueryAll_Click(object sender, EventArgs e)
{
Bind();
}
protected void btnFuzzy_Click(object sender, EventArgs e)
{
Response.Redirect("FuzzyQuery.aspx");
}
protected void btnOrder_Click(object sender, EventArgs e)
{
MyPetShopEntities db = new MyPetShopEntities();
var results = db.Table.OrderByDescending(p => p.Descn);
gvCategory.DataSource = results.ToList();
gvCategory.DataBind();
}
protected void btnGroup_Click(object sender, EventArgs e)
{
MyPetShopEntities db = new MyPetShopEntities();
var results = db.Table.GroupBy(p => p.Name);
foreach (var g in results)
{
if (g.Key == "hw")
{
var results2 = from r in g select r;
gvCategory.DataSource = results2.ToList();
gvCategory.DataBind();
}
}
}
protected void btnPolymerize_Click(object sender, EventArgs e)
{
MyPetShopEntities db = new MyPetShopEntities();
var results = db.Table.GroupBy(p => p.CategoryId).Select(g => new
{
Key = g.Key,
Count = g.Count(),
MaxDescn = g.Max(p => p.Descn)
});
gvCategory.DataSource = results.ToList();
gvCategory.DataBind();
}
}
FuzzQuery模糊查找
aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FuzzyQuery.aspx.cs" Inherits="FuzzyQuery" %>
<!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">
<div>
分类名:<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="bthSearch" runat="server" Text="搜索" OnClick="BtnSearch_Click" />
<asp:Button ID="btnReturn" runat="server" Text="返回" OnClick="BtnReturn_Click" />
<asp:GridView ID="gvCategory" runat="server">
</asp:GridView>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class FuzzyQuery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnSearch_Click(object sender, EventArgs e)
{
MyPetShopEntities db = new MyPetShopEntities();
var results = db.Table.Where(c => c.Name.Contains(txtSearch.Text)).ToList();
if(results.Count()!=0)
{
gvCategory.DataSource = results;
gvCategory.DataBind();
}
else
{
lblMsg.Text = "没有满足条件的数据!";
}
}
protected void BtnReturn_Click(object sender, EventArgs e)
{
Response.Redirect("DataManage.aspx");
}
}
Insert插入
aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Insert.aspx.cs" Inherits="Insert" %>
<!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">
.auto-style1 { width: 100%; }
.auto-style2 { width: 71px; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2" style="text-align: right">分类名:</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td class="auto-style2" style="text-align: right; vertical-align: top">描述:</td>
<td>
<asp:TextBox ID="txtDescn" runat="server" TextMode="MultiLine" Height="50px" Width="148px"></asp:TextBox></td>
</tr>
</table>
<asp:Button ID="btnInsert" runat="server" Text="插入" OnClick="BtnInsert_Click" />
<asp:Button ID="btnReturn" runat="server" Text="返回" OnClick="BtnReturn_Click" />
</div>
</form>
</body>
</html>
cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Insert : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnInsert_Click(object sender, EventArgs e)
{
MyPetShopEntities db = new MyPetShopEntities(); //定义MyPetShopEntities类实例db
Table category = new Table(); //Table是表名
category.Name = txtName.Text;
category.Descn = txtDescn.Text;
db.Table.Add(category); //插入实体category
db.SaveChanges(); //提交更改
}
protected void BtnReturn_Click(object sender, EventArgs e)
{
Response.Redirect("DataManage.aspx");
}
}