#.NET的博文案例练习

本文详细介绍了如何使用Entity Framework在C#中进行数据库article表与catelog表的操作,包括创建表结构、数据关联、CRUD操作以及前端页面的下拉框查询、详情查看和发布功能实现。

建立数据库

article表 文章表

列名数据类型说明
idint主键,自增
titlenvarchar(50)非空
authornvarchar(50)非空
contenttext非空
timedate非空
catelogidint非空

catelong表 文章类型表

列名数据类型说明
idint主键,自增
namevarchar(50)非空

建立数据外键关联

设置外键信息

创建EF实体数据模型导入数据

导入EF实体数据模型

前端静态页面实现

主页静态页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="_5月27号.index" %>

<!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>
        .div1 {
            width: 400px;
            height: 200px;
            margin:0px auto;
            text-align:center;
            float:left;
            cursor: pointer;
        }
        p {
            margin:0px auto;
            text-align:center;
        }
        .span2{
            float:right;
        }
        #wz{
            width:800px;
            height:500px
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div id="wz">
            
            <p><span class="span1">请选择类型:<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" CssClass="dro" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" OnTextChanged="DropDownList1_TextChanged"></asp:DropDownList></span>
            <span class="span2"><asp:LinkButton style="text-decoration:none;color:black;" ID="LinkButton2" runat="server">添加</asp:LinkButton></span></p>
            <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" Style="text-decoration:none;color:black;" runat="server" CommandName="xx" CommandArgument='<%# Eval("id")%>'>
                    <div class="div1">
                        <p class="div2">标    题:<%# Eval("title")%></p>
                        <p>作    者:<%# Eval("author")%></p>
                        <p>类    型:<%# Eval("catelong.name")%></p>
                        <p>内    容:<%# Eval("content")%></p>
                        <p>发布时间:<%# Convert.ToDateTime(Eval("time")).ToString("yyyy年MM月dd日")%></p>
                        <hr />
                    </div>
                        </asp:LinkButton>
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>
</html>

查看详情页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_5月27号.WebForm1" %>

<!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>
        	//<%= 值%>接收功能页面的值显示在此页面
            <p>标题:<h1><%= Title%></h1></p>
            <p>作者:<%= author%></p>
            <p>类型:<%= cname%></p>
            <p>内容:<%= content%></p>
            <p>发布时间:<%= time%></p>
            <asp:Button ID="Button1" runat="server" Text="返回" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>

发布文章页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="_5月27号.WebForm2" %>

<!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>
            <p>标题:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></p>
            <p>作者:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></p>
            <p>类型:<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList></p>
            <p>类容:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></p>
            <asp:Button ID="Button1" runat="server" Text="发布文章" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>

功能页面代码

主页面功能代码

	public partial class index : System.Web.UI.Page
    {
    	//实例化数据模型
        MyDBEntities db = new MyDBEntities();
        //加载事件实现功能
        protected void Page_Load(object sender, EventArgs e)
        {
        	//窗体加载第一次实现,预加载
            if (!IsPostBack)
            {
            	//获取添加页面返回的值
                string fag= Request.QueryString["fag"];
                //判断获取到的值是否正确,值为cg则显示添加成功,值为sb则显示添加失败
                if (fag=="cg")
                {
                    Response.Write("<script>alert('添加成功')</script>");
                }else if(fag=="sb"){
                	Response.Write("<script>alert('添加失败')</script>");
                }
                //查询数据模型类article表数据,绑定到repeat
                Repeater1.DataSource = db.article.ToList();
                Repeater1.DataBind();
                //查询数据模型类文章类型表数据,绑定到DropDownList
                DropDownList1.DataSource = db.catelong.ToList();
                //DataTextField绑定Name值,DataValueField 绑定id值
                DropDownList1.DataTextField = "name";
                DropDownList1.DataValueField = "id";
                DropDownList1.DataBind();
                //添加一个全部到DropDownList项内
                ListItem list = new ListItem();
                //text为全部
                list.Text = "全部";
                //value为0
                list.Value = "0";
                DropDownList1.Items.Insert(0,list);
            }
        }
        
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
        	//判断CommandName值为页面CommandName相等
            if (e.CommandName=="xx")
            {
            	//获取点击的id
                int id = int.Parse(e.CommandArgument.ToString());
                //跳转到详情页,把获取到的id传过去
                Response.Redirect($"WebForm1.aspx?id={id}");
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
        	//全部的value为0,判断DropDownList的text值为全部时显示全部数据
            if (DropDownList1.SelectedValue == "0")
            {
                Repeater1.DataSource = db.article.ToList();
                Repeater1.DataBind();
            }
            //不为全部时获取到DropDownList的value值则根据value查询显示数据
            else
            {
                int shu = int.Parse(DropDownList1.SelectedValue);
                Repeater1.DataSource = db.article.Where(a => a.catelogid == shu).ToList();
                Repeater1.DataBind();
            }
        }
        protected void LinkButton2_Click(object sender, EventArgs e)
        {
        	//跳转到发布文章页面
            Response.Redirect("WebForm2.aspx");
        }
    }

查看详情页面功能代码

		//实例化数据模型
		MyDBEntities db = new MyDBEntities();
		//定义值显示在详情页面
        public string title = "";
        public string author = "";
        public string cname = "";
        public string content = "";
        public string time = "";
        protected void Page_Load(object sender, EventArgs e)
        {
        	//获取从主页面传过来id
            int id = int.Parse(Request.QueryString["id"]);
            //根据从主页面传过来的id查询数据,把数据给到上面定义的值
            article b = db.article.Where(a => a.Id == id).Single();
            title = b.title;
            author = b.author;
            cname = b.catelogid.ToString();
            content = b.content;
            time = Convert.ToDateTime(b.time).ToString("yyyy年MM月dd日");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
        	//点击返回时返回到主页面
            Response.Redirect("index.aspx");
        }

发布文章功能代码

		MyDBEntities db = new MyDBEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
        	//预加载
            if (!IsPostBack)
            {
            	//将类型表的id和name绑定到DropDownList
                DropDownList1.DataSource = db.catelong.ToList();
                DropDownList1.DataTextField = "name";
                DropDownList1.DataValueField = "id";
                DropDownList1.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
        	//判断你所输入的值不能为空,为空则提示“你所输入的数据不完整”
        	if(TextBox1.Text!=""&&TextBox2.Text!=""&&TextBox3.Text!=""){
        		//实例化数据模型的article类把你写入的值给到article
        		article article = new article();
	            article.title = TextBox1.Text;
	            article.author = TextBox2.Text;
	            article.catelogid = int.Parse(DropDownList1.SelectedValue);
	            article.content = TextBox3.Text;
	            article.time = DateTime.Now;
	            db.article.Add(article);
	            //判断受影响的函数是否大于0,大于0返回一个cg的值,小于0返回一个sb的值
	            if (db.SaveChanges()>0)
	            {
	                string fag = "cg";
	                Response.Redirect($"index.aspx?fag={fag}");
	            }else{
	            	string fag = "sb";
	                Response.Redirect($"index.aspx?fag={fag}");
	            }
        	}
        }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泽痕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值