整體操作流程如下:
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è)表的主鍵和外鍵,操作圖如下
三讲逛、創(chuàng)建項(xiàng)目亏吝,搭建三層架構(gòu)
四、在Model層引用ADO.NET實(shí)體數(shù)據(jù)模型
右鍵Model盏混,選擇添加蔚鸥,選擇新建項(xiàng)目在彈出來的窗體中選擇數(shù)據(jù)或者Data,在選擇ADO.NET實(shí)體數(shù)據(jù)模型许赃。選擇新建連接選擇來自數(shù)據(jù)庫(kù)的EF設(shè)計(jì)器止喷,選擇你所需要的數(shù)據(jù)庫(kù),選擇你所需要的顯示的表混聊。點(diǎn)擊確認(rèn)即可完成启盛。
五、編寫DAL類技羔,編寫增刪查改僵闯,首先編寫查
1、查詢數(shù)據(jù)
public List<Article> Select()
? ? ? ? {
? ? ? ? ? ? MyDBEntities db = new MyDBEntities();
? ? ? ? ? ? /*var result = from article in db.Article
? ? ? ? ? ? ? ? ? ? ? ? select article;
? ? ? ? ? ? return result.ToList();*/
? ? ? ? ? ? //方法查詢函數(shù)
? ? ? ? ? ? return db.Article.Select(p => p).ToList();
? ? ? ? }
2藤滥、根據(jù)ID查詢
public Article Select(int id)
? ? ? ? {
? ? ? ? ? ? MyDBEntities db = new MyDBEntities();
? ? ? ? ? ? var result =(from article in db.Article
? ? ? ? ? ? ? ? ? ? ? ? where article.Id==id
? ? ? ? ? ? ? ? ? ? ? ? select article).Single();
? ? ? ? ? ? return result;
? ? ? ? }
3鳖粟、添加文章
public int Add(Article article)
? ? ? ? {
? ? ? ? ? ? MyDBEntities db = new MyDBEntities();
? ? ? ? ? ? db.Article.Add(article);
? ? ? ? ? ? //寫入數(shù)據(jù)庫(kù),保存寫入成功的記錄條數(shù)
? ? ? ? ? ? int count = db.SaveChanges();
? ? ? ? ? ? return count;
? ? ? ? }
4拙绊、刪除文章
? ?? public int Remove(Article article)
? ? ? ? {
? ? ? ? ? ? MyDBEntities db = new MyDBEntities();
? ? ? ? ? ? db.Article.Remove(article);
? ? ? ? ? ? int count = db.SaveChanges();
? ? ? ? ? ? return count;
? ? ? ? }
5向图、修改文章
public int Update(Article article)
? ? ? ? {
? ? ? ? ? ? MyDBEntities db = new MyDBEntities();
? ? ? ? ? ? //設(shè)置對(duì)象狀態(tài)
? ? ? ? ? ? db.Entry<Article>(article).State = System.Data.Entity.EntityState.Modified;
? ? ? ? ? ? int count = db.SaveChanges();
? ? ? ? ? ? return count;
? ? ? ? }
六、編寫B(tài)LL類
1标沪、添加一個(gè)私有字段榄攀,用于調(diào)數(shù)據(jù)訪問對(duì)象
public ArticleDAO aAdo = new ArticleDAO();
2、添加文章
public int AddArticle(Article article)
? ? ? ? {
? ? ? ? ? ? return aAdo.Add(article);
? ? ? ? }
3金句、查詢所有數(shù)據(jù)
public List<Article> Select()
? ? ? ? {
? ? ? ? ? ? return aAdo.Select();
? ? ? ? }
4檩赢、根據(jù)ID查詢單個(gè)數(shù)據(jù)
public Article Select(int id)
? ? ? ? {
? ? ? ? ? ? return aAdo.Select(id);
? ? ? ? }
5、添加文章违寞,添加數(shù)據(jù)
public int Add(Article article)
? ? ? ? {
? ? ? ? ? ? return aAdo.Add(article);
? ? ? ? }
七贞瞒、編寫視圖層? 右鍵web項(xiàng)目,選擇添加趁曼,選擇新建窗體军浆,轉(zhuǎn)到設(shè)計(jì)視圖,用工具欄拖控件到設(shè)計(jì)視圖完成界面設(shè)計(jì)挡闰,
1乒融、在Grdview控件綁定數(shù)據(jù)生成之后代碼如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
2、添加數(shù)據(jù)
<Columns>
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author" />
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Coutent" HeaderText="Coutent" SortExpression="Coutent" />
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="Catelogid" HeaderText="Catelogid" SortExpression="Catelogid" />
? ? ? ? ? ? ? ? ? ? <asp:BoundField DataField="PushDate" HeaderText="PushDate" SortExpression="PushDate" />
? ? ? ? ? ? ? ? </Columns>
3摄悯、連接BLL類自動(dòng)生成數(shù)據(jù)
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" TypeName="BLL.AritcleService"></asp:ObjectDataSource>
4赞季、加入一個(gè)Button按鈕
<asp:Button ID="Button1" runat="server" Text="發(fā)表文章" OnClick="Button1_Click"/>
八、添加頁(yè)面代碼
<div>
? ? ? ? ? ? 標(biāo)題:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
? ? ? ? ? ? <br />
? ? ? ? ? ? 作者:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
? ? ? ? ? ? <br />
? ? ? ? ? ? 文章內(nèi)容:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
? ? ? ? ? ? <br />
? ? ? ? ? ? 類別:<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="name" DataValueField="id"></asp:DropDownList>
? ? ? ? ? ? <br />
? ? ? ? ? ? <asp:Button ID="Button1" runat="server" Text="發(fā)表文章" OnClick="Button1_Click" />
? ? ? ? </div>
九射众、編寫視圖層后臺(tái)代碼
1沙兰、引用方法并添加跳轉(zhuǎn)后臺(tái)代碼
AritcleService aritcleService = new AritcleService();
? ? ? ? protected void Page_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (!IsPostBack)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //定義一個(gè)msg接收添加頁(yè)面?zhèn)鬟^來的msg
? ? ? ? ? ? ? ? string msg = Request.QueryString["msg"];
? ? ? ? ? ? ? ? if (!string.IsNullOrEmpty(msg))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Response.Write("<script>alert('添加成功!')</script>");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? 添加之后
? ? ? ? ? ? //if (!IsPostBack)
? ? ? ? ? ? //{
? ? ? ? ? ? //? ? Response.Write("<script>alert('新增成功阿迈!')</script>");
? ? ? ? ? ? //}
? ? ? ? }
? ? ? ? protected void Button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Response.Redirect("Add.aspx");
? ? ? ? }
十畦韭、添加后臺(tái)代碼
1、實(shí)例化DAL兩個(gè)類罗洗,展示數(shù)據(jù)愉舔,綁定數(shù)據(jù):
//實(shí)例化DAL兩個(gè)類
private CatelogService catelogService = new CatelogService();
? ? ? ? private AritcleService aritcleService = new AritcleService();
//綁定數(shù)據(jù),展示數(shù)據(jù)
? ? ? ? protected void Page_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (!IsPostBack)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//綁定數(shù)據(jù)
? ? ? ? ? ? ? ? this.DropDownList1.DataSource = catelogService.Select();
? ? ? ? ? ? ? ? ? ? ? ? ? //展示數(shù)據(jù)
? ? ? ? ? ? ? ? this.DropDownList1.DataBind();
? ? ? ? ? ? }
? ? ? ? }
2伙菜、在Button里面添加數(shù)據(jù)
protected void Button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Article article = new Article();
? ? ? ? ? ? article.Title = this.TextBox1.Text;
? ? ? ? ? ? article.Author = this.TextBox2.Text;
? ? ? ? ? ? article.Coutent = this.TextBox3.Text;
? ? ? ? ? ? article.Catelogid = int.Parse(this.DropDownList1.SelectedValue);
? ? ? ? ? ? article.PushDate = DateTime.Now;
? ? ? ? ? ? //聲明一個(gè)變量來接收?qǐng)?zhí)行后的結(jié)果
? ? ? ? ? ? int result= aritcleService.Add(article);
? ? ? ? ? ? //判斷是否添加成功
? ? ? ? ? ? if (result > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //成功了就執(zhí)行這段代碼
? ? ? ? ? ? ? ? //~/zhuye.aspx這是跳轉(zhuǎn)到的那個(gè)頁(yè)面? ?msg=添加成功給zhuye的msg賦值
? ? ? ? ? ? ? ? Response.Redirect("~/zhuye.aspx?msg=添加成功");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Response.Write("<script>alert('添加失敗')</script>");
? ? ? ? ? ? }
? ? ? ? ? ? //if (result>0)
? ? ? ? ? ? //{
? ? ? ? ? ? //? ? Response.Redirect("WebForm1.aspx");
? ? ? ? ? ? //}
? ? ? ? }
? ? }