1.建表 文章类别表
字段名 | 类型 | 约束 |
---|---|---|
ID | INT | 主键 自增 非空 唯一 |
Name | varchar(50) | 非空 |
Comment | varchar(200) |
文章表
字段名 | 类型 | 约束 |
---|---|---|
ID | INT | 主键 自增 非空 唯一 |
Title | varchar(100) | 非空 |
Author | varchar(10) | 非空 |
PushDate | date | 非空 default |
Conent | text | |
Catelogld | int | 外键约束 引用catelog表中的id |
2.添加测试数据
注意:创建好两个表之间的关联关系,要为两个表1设置主键,添加顺序是先加主表,再加子表。
3.搭建三层项目结构
·创建ASP.NET项目
·创建类库Model
·创建类库DAL
·创建类库BLL
1.添加ADO.NET实体数据模型
然后进行下列操作
选取你自己所用的数据库就行!
然后进行DAL的操作
·引用Model层,BLL层
·添加引用EntityFromework
·添加引用EntityFromework.sqlserver
·在Model层中找到App.config文件
<connectionStrings> <add name="changDBEntities" connectionString="metadata=res:///Model1.csdl|res:///Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=changDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings>
剪切放到web层里边web.onfig文件中。
创建新的窗体fault
<div>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Add.aspx">发表文章</asp:HyperLink>
<table>
<tr>
<th>标题</th>
<th>作者</th>
<th>日期</th>
<th>内容</th>
<th>类别</th>
</tr>
<asp:DropDownList ID="DropDownList1" runat="server" ></asp:DropDownList>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td><%# Eval("Title") %></td>
<td><%# Eval("Author") %></td>
<td><%# Eval("Data") %></td>
<td><%# Eval("Cont") %></td>
<td><%# Eval("Content") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
在ArticleDAO类中,添加增、删、改,查询等函数
查询函数
public class ArtDAO
{
ZuoEntities db = new ZuoEntities();
public object Select()
{
var result = (from a in db.Article
join c in db.Catelog
on a.Leibie equals c.ID
select new {a.Title,a.Author, a.Data,a.Cont,c.Content }).ToList();
return result;
}
public int Add(Article d)
{
db.Article.Add(d);
return db.SaveChanges();
}
repeater的控件绑定
ArtService se = new ArtService();
protected void Page_Load(object sender, EventArgs e)
{
this.Repeater1.DataSource = se.Select();
this.Repeater1.DataBind();
}
再添加控件hyperlink,设置属性【跳转属性】
创建添加Add
及下拉框控件
<h1>添加文章</h1>
标题:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
作者:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
内容:<asp:TextBox ID="TextBox3" runat="server" Rows="2" TextMode="MultiLine"></asp:TextBox><br />
类别:<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Content" DataValueField="ID">
</asp:DropDownList><br />
<asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />
最后Add中绑定及button的点击事件
public partial class Add : System.Web.UI.Page
{
ArtService art = new ArtService();
CatelogService cat = new CatelogService();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DropDownList1.DataSource = cat.Select();
this.DropDownList1.DataBind();
}
}
Article a = new Article();
a.Title = this.TextBox1.Text;
a.Author = this.TextBox2.Text;
a.Cont = this.TextBox3.Text;
a.Leibie = int.Parse(this.DropDownList1.SelectedValue);
int count = art.Add(a);
if (count > 0)
{
Response.Write("<script>alert('添加成功')</script>");
}
刷新
BLL中调用
return da.Select(id);
DAL中添加
var result = (from a in db.Article
join c in db.Catelog
on a.Leibie equals c.ID
where c.ID == id
select new { a.Title, a.Author, a.Data, a.Cont, c.Content }).ToList();
return result;
视图层中添加DDL、并取消乱码、添加AutoPostBack、OnSelectedIndexChanged。