整體操作流程如下:
1泌辫、創(chuàng)建完整SQL Server數(shù)據(jù)庫(kù)和表
2阳堕、引用ADO.NET實(shí)體數(shù)據(jù)模型
3、創(chuàng)建EF項(xiàng)目
4蒸殿、編輯DAL類
5筷厘、編輯BLL類
6鸣峭、編寫頁(yè)面代碼
7、編寫視圖后層代碼
8酥艳、添加界面后臺(tái)代碼
9摊溶、運(yùn)程序,修改bug充石,運(yùn)行成功
一莫换、新建數(shù)據(jù)庫(kù),新建兩個(gè)表
二骤铃、設(shè)置兩個(gè)表的主鍵和外鍵拉岁,操作圖如下
表格名字不同,請(qǐng)使用自己的表名
三惰爬、創(chuàng)建項(xiàng)目喊暖,搭建三層架構(gòu)
四、在Model層引用ADO.NET實(shí)體數(shù)據(jù)模型
右鍵Model撕瞧,選擇添加陵叽,選擇新建項(xiàng)目在彈出來(lái)的窗體中選擇數(shù)據(jù)或者Data,在選擇ADO.NET實(shí)體數(shù)據(jù)模型丛版。選擇新建連接選擇來(lái)自數(shù)據(jù)庫(kù)的EF設(shè)計(jì)器巩掺,選擇你所需要的數(shù)據(jù)庫(kù),選擇你所需要的顯示的表硼婿。點(diǎn)擊確認(rèn)即可完成锌半。
五禽车、編寫DAL類寇漫,編寫增刪查改,
1殉摔、查詢州胳,以下有兩種方式
? ? ?查詢所有的表信息
? ? ?public List<Article> Select()
? ? ? ? {
? ? ? ? ? ? //第一種方式
? ? ? ? ? ? /*var reslut = (from u in twoDown.Article
? ? ? ? ? ? ? ? ? ? ? ? ? select u).ToList();
? ? ? ? ? ? return reslut;*/
? ? ? ? ? ? //第二種方式
? ? ? ? ? ? return twoDown.Article.Select(p => p).ToList();
? ? ? ? }
2、 根據(jù)類別查詢
public List<Article> SelectByCatelogId(int id)
? ? ? ? {
? ? ? ? ? ? var reslut = from u in twoDown.Article
? ? ? ? ? ? ? ? ? ? ? ? where u.Typt == id
? ? ? ? ? ? ? ? ? ? ? ? select u;
? ? ? ? ? ? return reslut.ToList();
? ? ? ? }
3逸月、查詢作者栓撞,以下兩種方式
public List<Article> Select(string art)
? ? ? ? {
? ? ? ? ? ? //第一種方式
? ? ? ? ? ? /*var reslut = (from u in twoDown.Article
? ? ? ? ? ? ? ? ? ? ? ? ? select u).ToList();
? ? ? ? ? ? return reslut;*/
? ? ? ? ? ? //第二種方式
? ? ? ? ? ? return twoDown.Article.Select(p => p).ToList();
? ? ? ? }
4、多表進(jìn)行連接
public object Query()
? ? ? ? {
? ? ? ? ? ? var reslut = (from a in twoDown.Article
? ? ? ? ? ? ? ? ? ? ? ? ? join c in twoDown.Catelog on a.Typt equals c.Id
? ? ? ? ? ? ? ? ? ? ? ? ? select new {編號(hào)=a.Id ,標(biāo)題=a.Title,作者= a.Anthor,內(nèi)容= a.Content,類別= a.Typt,Typt=c.TypeName });
? ? ? ? ? ? return reslut.ToList();
? ? ? ? }
5碗硬、根據(jù)ID查詢表信息
public Article Select(int id)
? ? ? ? {
? ? ? ? ? ? var reslut = (from u in twoDown.Article
? ? ? ? ? ? ? ? ? ? ? ? ? where u.Typt == id
? ? ? ? ? ? ? ? ? ? ? ? ? select u).Single();
? ? ? ? ? ? return reslut;
? ? ? ? }
6瓤湘、添加文章????
public int Add(Article article)
? ? ? ? {
? ? ? ? ? ? twoDown.Article.Add(article);
? ? ? ? ? ? int count = twoDown.SaveChanges();
? ? ? ? ? ? return count;
? ? ? ? }
7、根據(jù)ID刪除指定用戶的信息
public int DeleteById(int id)
? ? ? ? {
? ? ? ? ? ? var reslut = (from u in twoDown.Article
? ? ? ? ? ? ? ? ? ? ? ? where u.Id == id
? ? ? ? ? ? ? ? ? ? ? ? select u).FirstOrDefault();
? ? ? ? ? ? twoDown.Article.Remove(reslut);
? ? ? ? ? ? return twoDown.SaveChanges();
? ? ? ? }
8恩尾、刪除
public int Delete(Article article)
? ? ? ? {
? ? ? ? ? ? twoDown.Article.Remove(article);
? ? ? ? ? ? int count = twoDown.SaveChanges();
? ? ? ? ? ? return count;
? ? ? ? }
9弛说、修改
public int Update(Article article)
? ? ? ? {
? ? ? ? ? ? twoDown.Entry<Article>(article).State = EntityState.Modified;
? ? ? ? ? ? int count = twoDown.SaveChanges();
? ? ? ? ? ? return count;
? ? ? ? }
六、編寫另外一個(gè)DAL類
private TwoDownEntities TD = new TwoDownEntities();
? ? ? ? public List<Catelog> Select()
? ? ? ? {
? ? ? ? ? ? var reslut = from c in TD.Catelog
? ? ? ? ? ? ? ? ? ? ? ? select c;
? ? ? ? ? ? return reslut.ToList();
? ? ? ? }
七翰意、編寫B(tài)LL類
1木人、添加一個(gè)私有字段信柿,用于調(diào)數(shù)據(jù)訪問對(duì)象
public ArticleDAO aAdo = new ArticleDAO();
2、查詢所有表信息
public List<Article> Select()
? ? ? ? {
? ? ? ? ? ? return Articles.Select();
? ? ? ? }
? ? ? ?3醒第、查詢根據(jù)作者
? ?public List<Article> Select(string art)
? ? ? ? {
? ? ? ? ? ? return Articles.Select(art);
? ? ? ? }
? ? ? ? 4渔嚷、根據(jù)類別查詢
? ? ? public List<Article> SelectByCatelogId(int id)
? ? ? ? {
? ? ? ? ? ? return Articles.SelectByCatelogId(id);
? ? ? ? }
? ? ? ?5、 多表連接查詢
? ? ? ? public object Query()
? ? ? ? {
? ? ? ? ? ? return Articles.Query();
? ? ? ? }
? ? ? ?6稠曼、 根據(jù)id查詢信息
? ? ? ?public Article Select(int id)
? ? ? ? {
? ? ? ? ? ? return Articles.Select(id);
? ? ? ? }
? ? ?7形病、 添加文章
? ? ?public int AddArticle(Article article)
? ? ? ? {
? ? ? ? ? ? return Articles.Add(article);
? ? ? ? }
? ? ? ?8、根據(jù)id來(lái)刪除
? ? ? ?public int DeleteById(int id)
? ? ? ? {
? ? ? ? ? ? return Articles.DeleteById(id);
? ? ? ? }
? ? ?9霞幅、 刪除
? ? ? public int Delete(Article article)
? ? ? ? {
? ? ? ? ? ? return Articles.Delete(article);
? ? ? ? }
? ? ? 10? 窒朋、 修改
? ? public int Update(Article article)
? ? ? ? {
? ? ? ? ? ? return Articles.Update(article);
? ? ? ? }
? ? ? ?11、編輯
? ? ? ? public int Update(int Id,string Title,string Anthor,string Content,int Typt,string Sun)
? ? ? ? {
? ? ? ? ? ? Article article = new Article();
? ? ? ? ? ? article.Id = Id;
? ? ? ? ? ? article.Title = Title;
? ? ? ? ? ? article.Anthor = Anthor;
? ? ? ? ? ? article.Content = Content;
? ? ? ? ? ? article.Typt = Typt;
? ? ? ? ? ? article.Sun = Sun;
? ? ? ? ? ? return Articles.Update(article);
? ? ? ? }
八蝗岖、在另外一個(gè)BLL類編寫代碼侥猩,如下
private CatelogDAO Catelog = new CatelogDAO();
? ? ? ? public List<Catelog> Select()
? ? ? ? {
? ? ? ? ? ? return Catelog.Select();
? ? ? ? }
九、編寫視圖層?右鍵web項(xiàng)目抵赢,選擇添加欺劳,選擇新建窗體,轉(zhuǎn)到設(shè)計(jì)視圖铅鲤,用工具欄拖控件到設(shè)計(jì)視圖完成界面設(shè)計(jì)划提,
1、首先邢享,先編寫添加信息和作者代碼
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Add.aspx">添加信息</asp:HyperLink>
? ? ? ? ? ? 作者:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
2鹏往、在Grdview控件綁定數(shù)據(jù)生成之后代碼如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" DataKeyNames="Id">
? ? ? ? ? ? ? ? <Columns>
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Id" HeaderText="編號(hào)" SortExpression="Id" />
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Title" HeaderText="標(biāo)題" SortExpression="Title" />
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Anthor" HeaderText="作者" SortExpression="Anthor" />
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Content" HeaderText="內(nèi)容" SortExpression="Content" />
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Typt" HeaderText="類別" SortExpression="Typt" />
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Sun" HeaderText="日期" SortExpression="Sun" />
? ? ? ? ? ? ? ? ? ? <asp:CommandField ShowDeleteButton="True" HeaderText="刪除" />
? ? ? ? ? ? ? ? ? ? <asp:CommandField ShowEditButton="True"? HeaderText="編輯" />
? ? ? ? ? ? ? ? </Columns>
? ? ? ? ? ? </asp:GridView>
3、在加入以下數(shù)據(jù)
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" TypeName="BLL.ArticleSerivce" DeleteMethod="DeleteById" UpdateMethod="Update">
? ? ? ? ? ? ? ? <DeleteParameters>
? ? ? ? ? ? ? ? ? ? <asp:Parameter Name="id" Type="Int32" />
? ? ? ? ? ? ? ? </DeleteParameters>
? ? ? ? ? ? ? ? <UpdateParameters>
? ? ? ? ? ? ? ? ? ? <asp:Parameter Name="Id" Type="Int32" />
? ? ? ? ? ? ? ? ? ? <asp:Parameter Name="Title" Type="String" />
? ? ? ? ? ? ? ? ? ? <asp:Parameter Name="Anthor" Type="String" />
? ? ? ? ? ? ? ? ? ? <asp:Parameter Name="Content" Type="String" />
? ? ? ? ? ? ? ? ? ? <asp:Parameter Name="Typt" Type="String" />
? ? ? ? ? ? ? ? ? ? <asp:Parameter Name="Sun" Type="String" />
? ? ? ? ? ? ? ? </UpdateParameters>
? ? ? ? ? ? </asp:ObjectDataSource>
十骇塘、在下面這個(gè)視圖層編寫網(wǎng)頁(yè)
<div>
? ? ? ? ? ? <b>添加文章</b>
? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? <div class="cols-4">
? ? ? ? ? ? ? ? ? ? 標(biāo)題:
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? <div class="cols-8">
? ? ? ? ? ? ? ? ? ? <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? <div class="cols-4">
? ? ? ? ? ? ? ? ? ? 作者:
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? <div class="cols-8">
? ? ? ? ? ? ? ? ? ? <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? <div class="cols-4">
? ? ? ? ? ? ? ? ? ? 內(nèi)容:
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? <div class="cols-8">
? ? ? ? ? ? ? ? ? ? <asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine"></asp:TextBox>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? <div class="cols-4">
? ? ? ? ? ? ? ? ? ? 類型:
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? <div class="cols-8">
? ? ? ? ? ? ? ? ? ? <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="TypeName" DataValueField="Id" ></asp:DropDownList>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? <div class="cols-4">
? ? ? ? ? ? ? ? ? ? 時(shí)間:
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? <div class="cols-8">
? ? ? ? ? ? ? ? ? ? <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? <div class="cols-4">
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? <div class="cols-8">
? ? ? ? ? ? ? ? ? ? <asp:Button ID="Button1" runat="server" Text="發(fā)表文章" OnClick="Button1_Click" />
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
十一伊履、在這個(gè)視圖層加入類別,連接數(shù)據(jù)
<div class="row">
? ? ? ? ? ? ? ? <label class="cols-5">類別:</label>
? ? ? ? ? ? ? ? <div class="cols-5">
? ? ? ? ? ? ? ? ? ? <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="TypeName" DataValueField="Id" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? ? ? <asp:Repeater ID="Repeater1" runat="server">
? ? ? ? ? ? ? ? <ItemTemplate>
? ? ? ? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? ? ? ? ? <div class="row"></div>
? ? ? ? ? ? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? ? ? ? ? ? ? <label class="cols-5">作者:<%# Eval("Anthor") %></label>
? ? ? ? ? ? ? ? ? ? ? ? ? ?    <label class="cols-5">發(fā)布時(shí)間:<%# Eval("Sun") %></label></div>
? ? ? ? ? ? ? ? ? ? ? ? <div class="row">
? ? ? ? ? ? ? ? ? ? ? ? ? ? <%# Eval("Content") %>
? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? </ItemTemplate>
? ? ? ? ? ? </asp:Repeater>