C#EntityFramework 对SQL数据库进行增删改操作(稍微有点基础就可以看懂,按这个写出来,每一步很详细,所有需要的代码都贴出来了)
使用EntityFramework访问数据库可以减少以前搭建三层架构书写代码的工作量,减轻开发的时间。
数据表的设计
这里给的图就不一一描述字段意思了
创建Web项目(.NET Framework)
项目创建好之后,右键项目找到 > 添加 > 新建项目 > Visual C# 数据选项 >ADO.NET 实体数据模型 > 添加 >下一步(这里就用第一个模型)
下一步就到了这里需要点击 新建连接
点击 新建连接之后 填写好这些地方记得保存密码,不然后期会登录"sa"失败,以及后面不引入程序集 System.Data.Linq 也会出现登录"sa"失败
确定之后 随便勾选是否,下一步
选择数据源,把表这个选项构上
完成之后,项目引入程序集。
点击项目的 >引用 >程序集 > 搜索 linq >在列表中勾选 System.Data.Linq >确定
现在只差代码了!
前端代码:
<body>
<form id="form1" runat="server">
<div style="display:flex;">
<asp:HiddenField ID="HiddenField1" runat="server" />
<table border="1">
<tr>
<th>标题</th>
<th>作者</th>
<th>内容</th>
<th>类型</th>
<th>操作</th>
</tr>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr>
<th><%# Eval("title") %></th>
<th><%# Eval("author") %></th>
<th><%# Eval("content") %></th>
<th><%# Eval("catrlogid") %></th>
<th>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="del" CommandArgument='<%# Eval("title") %>'>删除</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="update" CommandArgument='<%# Eval("id") %>'>修改</asp:LinkButton>
</th>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<div style="width:500px;height:500px;display:flex;margin-left:5%;">
<div>
<asp:Label ID="Label1" runat="server" Text="标题"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="作者"></asp:Label><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="内容"></asp:Label><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<asp:Label ID="Label4" runat="server" Text="类型"></asp:Label><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br />
<asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />
</div>
<div style="margin-left:5%;">
<asp:Label ID="Label5" runat="server" Text="标题"></asp:Label><asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
<asp:Label ID="Label6" runat="server" Text="作者"></asp:Label><asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
<asp:Label ID="Label7" runat="server" Text="内容"></asp:Label><asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br />
<asp:Label ID="Label8" runat="server" Text="类型"></asp:Label><asp:DropDownList ID="DropDownList2" runat="server" ></asp:DropDownList><br />
<asp:Button ID="Button2" runat="server" Text="修改" OnClick="Button2_Click"/>
</div>
</div>
</div>
</form>
</body>
前端代码生成界面:
后端代码:
//Web窗体类后面的所有代码
MyDBEntities2 db = new MyDBEntities2();//引用实体模型,这里是连接建好以后筛选数据下面文本框得命名 数据库名+Entities(第一个),后面第二个就是Entities2 以此类推
// 是注释
protected void Page_Load(object sender, EventArgs e)
{
//回发
if (!IsPostBack)
{
Data();
DropData();
}
}
//数据显示
public void Data()
{
//LINQ查询数据语句
var data = from c in db.Catelog
join a in db.Article on c.id equals a.catrlogid
select new
{
id = a.id,
title = a.title,
author = a.author,
content = a.content,
catrlogid = c.name
};
Repeater1.DataSource = data.ToList();//绑定数据源,ToList()把数据转换为 list集合
Repeater1.DataBind();//显示绑定的数据
}
//下拉框数据
public void DropData()
{
var dataDrop = from c in db.Catelog
select new
{
id = c.id,
name = c.name
}
;
DropDownList1.DataSource = dataDrop.ToList();//绑定数据源,ToList()把数据转换为 list集合
DropDownList1.DataTextField = "name";//列表项可见部分文本
DropDownList1.DataValueField = "id";//列表项的值部分
DropDownList1.DataBind();//显示绑定的数据
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "")
{
Response.Write("<script>alert('请输入完整')</script>");
return;
}
else
{
var data = from a in db.Article
where a.title == TextBox1.Text
select a.title;
foreach (var item in data)
{
if (item == TextBox1.Text)
{
Response.Write("<script>alert('已存在,请输入其他标题')</script>");
return;
}
else
{
break;
}
}
int cid = int.Parse(DropDownList1.SelectedValue);//获取到选定项的值 DropDownList1.DataValueField = "id";列表项的值部分
Article ar = new Article(TextBox1.Text, TextBox2.Text, TextBox3.Text, cid);//创建对象保存需要添加到数据库的值
db.Article.Add(ar);
if (db.SaveChanges() > 0)
{
Response.Write("<script>alert('添加成功')</script>");
TextBox1.Text = "";//清空文本框
TextBox2.Text = "";//清空文本框
TextBox3.Text = "";//清空文本框
Data();//调用数据显示方法重新显示
DropData();//调用下拉框数据方法
}
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "del")
{
string title = e.CommandArgument.ToString();//获取删除所需标题,ID也可以
db.Article.Remove(db.Article.FirstOrDefault(n => n.title == title));
if (db.SaveChanges() > 0)
{
Response.Write("<script>alert('删除成功')</script>");
Data();
DropData();
}
}
if (e.CommandName == "update")
{
aid = int.Parse(e.CommandArgument.ToString());//获取修改所需ID
HiddenField1.Value = aid.ToString();//给隐藏控件的赋值
var data = from a in db.Article
where a.id == aid
select a;
foreach (var item in data.ToList())
{
TextBox4.Text = item.title;
TextBox5.Text = item.author;
TextBox6.Text = item.content;
int id = (int)item.catrlogid;
//DropDownList1.Text = item.catrlogid;
DropData(id);
DropDownList2.Items[id - 1].Selected = true;//选中传过来的下拉框类型名
}
}
}
public int aid;//全局变量接受修改作者ID
public void DropData(int id)
{
var dataDrop = from c in db.Catelog
select new
{
newid = c.id,
name = c.name
};
;
DropDownList2.DataSource = dataDrop.ToList();
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "newid";
DropDownList2.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox4.Text == "" || TextBox5.Text == "" || TextBox6.Text == "")
{
Response.Write("<script>alert('请输入完整')</script>");
return;
}
else
{
if (HiddenField1.Value == "")
{
Response.Write("<script>alert('没找到修改的数据源')</script>");
return;
}
else
{
int hidden = int.Parse(HiddenField1.Value);
var updateData = db.Article.FirstOrDefault(n => n.id == hidden);
updateData.title = TextBox4.Text;
updateData.author = TextBox5.Text;
updateData.content = TextBox6.Text;
updateData.catrlogid = int.Parse(DropDownList2.SelectedValue);
int count = db.SaveChanges();//入库返回值
if (count > 0)
{
Response.Write("<script>alert('修改成功')</script>");
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
Data();
}
}
}
}
后端功能:
所有数据显示
删除
删除后的数据
添加和修改的输入完整:
添加标题相同提示:
主要是查重
没点修改会找不到用户
代码关键部分有注释,以上就是用EF对数据库的增删改操作。
第一次写博客,没经验,欢迎留言讨论。
联系QQ:2976258702