小陳解說
首先要?jiǎng)?chuàng)建好數(shù)據(jù)庫(kù)
create database? MyDB
然后創(chuàng)建和Article這兩個(gè)表Catelog
--創(chuàng)建Catelog表create? table? Catelog(
Id int identity(1,1)? primarykey,
Name nvarchar(50),
Comment text,
)
--創(chuàng)建Article表create? table?? Article(?? Id int identity(1,1)? primarykey,
?Title? nvarchar(50),
?Author?? nvarchar(50),
Content?? text,
Time?? datetime?? default(getdate()),
CatelogId? int?? references? Catelog(Id)?? )
然后在兩個(gè)表中加入幾條測(cè)試數(shù)據(jù)
首先打開vs
創(chuàng)建ASP腐缤。Net Web應(yīng)用程序(.NET Framework)項(xiàng)目
之后正確創(chuàng)建三層架構(gòu)和正確的添加引用
創(chuàng)建三個(gè)類庫(kù) Model(數(shù)據(jù)模型層)硼瓣,DAL(數(shù)據(jù)訪問層)坛怪,BLL(業(yè)務(wù)邏輯層)
DAL層引用Model層
BLL引用DAL和Model
web引用BLL以及Model
Model(模型層)
之后在Model層中正確的添加數(shù)據(jù)ADO.Net實(shí)體數(shù)據(jù)模型
右鍵點(diǎn)擊Model添加新建項(xiàng)
找到ADO.Net實(shí)體數(shù)據(jù)模型
選擇來自數(shù)據(jù)庫(kù)的EF設(shè)計(jì)器
點(diǎn)擊下一步來到咱們的數(shù)據(jù)連接
先點(diǎn)新建連接,然后完成身份驗(yàn)證,找到要連接數(shù)據(jù)庫(kù)后點(diǎn)擊下一步
然后選擇實(shí)體框架6.x 繼續(xù)點(diǎn)擊下一步
點(diǎn)開表找到自己要用到的表點(diǎn)擊完成
當(dāng)出現(xiàn)下面這個(gè)就是創(chuàng)建成功了
還有一個(gè)最重要的是迈喉,打開Model層中的App.Config
找到下面代碼
<connectionStrings>
<addname="zhiboDBEntities"connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;providerconnectionstring="datasource=.;initialcatalog=zhiboDB;integratedsecurity=True;MultipleActiveResultSets=True;App=EntityFramework""providerName="System.Data.EntityClient"/>
</connectionStrings>
然后剪切到web(視圖層)的web.onfig文件中就可以啦
接下來就是DAL(數(shù)據(jù)訪問層)
首先在Model層的引用中找到EntityFramework觅捆,然后右鍵叮雳,在對(duì)象瀏覽器中查看EntityFramework的路徑
然后復(fù)制這個(gè)路徑,在DAL層添加引用妇汗,點(diǎn)擊瀏覽帘不,黏貼之前復(fù)制的路徑,找到EntityFramework和EntityFramework.sqlserver這兩個(gè)文件杨箭,引用就好了寞焙。
使用同樣的方法在web(視圖層)也添加EntityFramework和EntityFramework.sqlserver這兩個(gè)文件的引用
數(shù)據(jù)訪問層
接下來是在DAL層添加DAO類并開始編寫代碼
新建DAO類
先實(shí)例化模型層
MyDBEntities db = new MyDBEntities();
寫入頁(yè)面查詢顯示的方法
? ? ? ? public object Select()
? ? ? ? {
? ? ? ? ? ? return db.Article.Select(p => p).ToList();
? ? ? ? }
然后再寫入添加數(shù)據(jù)的方法
public? int??? Add(Article a ,Catelog c)
{? ?
? ? ? db.Article.Add(a);??
? ? ? ? db.Catelog.Add(c);? ?
? ? ? ? a.CatelogId = c.Id;
returndb.SaveChanges();??
? ? }
再寫入添加頁(yè)面的類型值的下拉框的值方法
public?? object? Select()
{
var? u=from? cin? db.Catelog?
selectc.Name;
return? u;??
? ? }
業(yè)務(wù)邏輯層
BLL層沒有很復(fù)雜的東西,調(diào)用DAO類中我們之前寫的方法就行了
首先是添加service類
然后實(shí)例化DAO
之后就是調(diào)用互婿,代碼如下
DAO da =new? DAO();?
調(diào)用數(shù)據(jù)訪問層的Select方法
實(shí)現(xiàn)主頁(yè)面顯示數(shù)據(jù)
public? object? Select()
{
return da.Select();? ? ?
? }
調(diào)用數(shù)據(jù)訪問層的Add方法
實(shí)現(xiàn)添加返回行數(shù)確認(rèn)
public? bool? Add(Article a, Catelog c)
{
if(da.inset(a,c) !=0)? ? ?
?{
return? true;? ? ? ? ??
}else{
return false;? ? ? ??
? }? ?
? ? }
調(diào)用數(shù)據(jù)訪問層的XialaSelect方法
實(shí)現(xiàn)添加頁(yè)面的類型的值的集合
public?? object? Select(){
return? da.Select();?
?? }
接下來就是視圖層了
創(chuàng)建Select.aspx 和Add.aspx 窗體文件
先添加一個(gè)下拉框控件DropDownList和數(shù)據(jù)控件Repeater
<asp:Repeater ID="Repeater1" runat="server">
? ? ? ? ? ? ? ? <ItemTemplate>
? ? ? ? ? ? ? ? ? ? <div id="app1">
? ? ? ? ? ? ? ? ? ? ? ? <div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <h1><b><%# Eval ("Title") %></b></h1><br />
? ? ? ? ? ? ? ? ? ? ? ? ? ? <hr />
? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? ? ? <div>
? ? ? ? ? ? ? ? ? ? ? ? ? <span>作者:<%# Eval ("Author")
%></span><span>發(fā)表時(shí)間:<%# Eval ("Datetime")
%></span><br /><br /></div>
? ? ? ? ? ? ? ? ? ? ? ? <div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <asp:Label ID="Label1" runat="server" ><%# Eval ("Content") %></asp:Label>
? ? ? ? ? ? ? ? ? ? ? ? ? <asp:LinkButton ID="LinkButton1"
runat="server" CommandName="di" CommandArgument='<%# Eval("ID")
%>' ForeColor="#ccffff"><%# Eval("Content")
%></asp:LinkButton>
? ? ? ? ? ? ? ? ? ? ? ? </div><br /><br />
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? </ItemTemplate>
? ? ? ? ? ? </asp:Repeater>
再通過下面代碼綁定數(shù)據(jù)源
protected void Page_Load(object sender, EventArgs e)
? ? ? ? {?
? ? ? ? ? ? if (!IsPostBack)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this.DropDownList1.DataSource = services.Selectbyname();
? ? ? ? ? ? ? ? this.DropDownList1.DataBind();
this.Repeater1.DataSource = services.Select();
? ? ? ? ? ? ? ? this.Repeater1.DataBind();
}
? ? ? ? }
然后寫一個(gè)添加頁(yè)面(Add.aspx)的跳轉(zhuǎn)地址
在主頁(yè)面添加HyperLink控件捣郊,并編輯控件的NavigateUrl屬性
添加頁(yè)面
在Add.aspx窗體頁(yè)面添加需要添加的布局
標(biāo)題? ?
內(nèi)容
作者
類型
類型要用到下拉框,這就需要用到我們之前寫的查詢所有類型的方法了
然后將下拉框數(shù)據(jù)進(jìn)行綁定
然后點(diǎn)擊設(shè)計(jì)慈参,在DropDownList上綁定數(shù)據(jù)源
再寫提交按鈕的單擊事件
protected?? void??? Button1_Click? (objectsender, EventArgs e){??
? ? ? ? ? Article a =newArticle();? ??
? ? ? Catelog c =newCatelog();? ? ?
? ? ? a.Title =this.TextBox1.Text;? ? ?
? ? ? a.Content =this.TextBox2.Text;??
? ? ? ? a.Author =this.TextBox3.Text;? ??
? ? ? c.Name =this.TextBox4.Text;? ? ? ??
? a.Time = DateTime.Now;if(hj.inset(a,c))? ? ? ? ? ? {??
? ? ? ? ? ? Response.Redirect("~/select.aspx?msg=添加成功");? ?
? ? ? ? }else{? ? ? ??
? ? ? Response.Write("alert(添加失敗')");? ? ??
? ? }? ? ?
? }
在然后添加成功會(huì)跳轉(zhuǎn)到查詢表頁(yè)面并且顯示出來
現(xiàn)在寫添加頁(yè)面的后臺(tái)代碼呛牲,如下
protected? void?? Page_Load(objectsender, EventArgs e){
if(!IsPostBack)? ? ? ? ??
{
string? msg = Request.QueryString["msg"];
if(!string.IsNullOrEmpty(msg))? ? ? ? ? ? ?
? {? ? ? ? ? ??
? ? ? Response.Write("? alert('添加成功')");? ? ? ?
? ? ? ? }? ? ? ??
? }? ? ??
}
成功后會(huì)顯示添加成功
接下來來添加下拉框,通過下拉框選擇類型進(jìn)行篩選
先找到屬性驮配,取消亂碼情況
在DropDownList控件中添加兩個(gè)屬性娘扩,一個(gè)是AutoPostBack,另一個(gè)是OnSelectedIndexChanged
將AutoPostBack里邊的值設(shè)置為true
在屬性O(shè)nSelectedIndexChanged添加一個(gè)方法
在方法中具體綁定代碼如下:
var data= se.Select(int.Parse(this.DropDownList1.SelectedValue));
this.Repeater1.DataSource =data;
this.Repeater1.DataBind();
接下來來寫查看詳情
首先在Add.aspx中LinkButton控件里邊的兩個(gè)很重要的屬性一個(gè)是CommandName這個(gè)屬性是命名僧凤,我們就用Name畜侦, 另一個(gè)是CommandArgument這個(gè)是綁定的ID,及待會(huì)要進(jìn)行的ID查看躯保,也是進(jìn)行傳值
在repeater控件中添加一個(gè)屬性O(shè)nItemCommand旋膳,該屬性是用來寫方法的
創(chuàng)建新窗體xiangqing.aspx
<div id="app">
? ? ? ? ? ? <asp:Repeater ID="Repeater1" runat="server">
? ? ? ? ? ? ? ? <ItemTemplate>
? ? ? ? ? ? ? ? ? ? <div id="app1">
? ? ? ? ? ? ? ? ? ? ? ? <div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <h1><b><%# Eval ("Title") %></b></h1><br />
? ? ? ? ? ? ? ? ? ? ? ? ? ? <hr />
? ? ? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? ? ? <div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <span>作者:<%# Eval ("Author") %></span><span>發(fā)表時(shí)間:<%# Eval ("Datetime") %></span><br /><br /></div>
? ? ? ? ? ? ? ? ? ? ? ? <div>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <asp:Label ID="Label1" runat="server" ><%# Eval ("Content") %></asp:Label>
? ? ? ? ? ? ? ? ? ? ? ? </div><br /><br />
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? </ItemTemplate>
? ? ? ? ? ? </asp:Repeater>
? ? ? ? </div>
在再加載事件中綁定數(shù)據(jù)源
service services = new service();
? ? ? ? protected void Page_Load(object sender, EventArgs e)
? ? ? ? {
ArtService se = new ArtService();
protected void Page_Load(object sender, EventArgs e) {
int id = int.Parse(Request.QueryString["id"]);
this.Repeater1.DataSource = se.Sel(id);
this.Repeater1.DataBind();
}
? ? ? ? }