目录
数据模型添加过程:
1,数据,新建
2,来自数据库的EF设计器,下一步![](https://i-blog.csdnimg.cn/blog_migrate/bcdcadcc0e16f5b1b49a09439ec137a4.png)
3,新建连接,设置数据源
4设置服务器,选择数据库,测试连接,确定
5,下一步
6,选中表,第一个选择打勾,第二个取消打勾,完成
7,遇见这个提示确定就好
效果图:
功能:
页面加载代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GoodsDBEntities db = new GoodsDBEntities(); List<Good> list = db.Goods.ToList(); this.Repeater1.DataSource = list; this.Repeater1.DataBind(); } }
模糊查找功能代码:
protected void Button1_Click(object sender, EventArgs e)
{
string text = this.TextBox1.Text;
GoodsDBEntities db = new GoodsDBEntities();
List<Good> list = db.Goods.Where(m => m.GoodName.Contains(text)).ToList();
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
添加代码:
protected void Button3_Click(object sender, EventArgs e)
{
string name = this.TextBox3.Text;
decimal price = decimal.Parse(this.TextBox4.Text);
Good model = new Good();
model.GoodName = name;
model.GoodPrice = price;
GoodsDBEntities db = new GoodsDBEntities();
db.Goods.Add(model);
db.SaveChanges();
List<Good> list = db.Goods.ToList();
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
修改价格代码:
protected void Button4_Click(object sender, EventArgs e)
{
int id = int.Parse(this.TextBox5.Text);
decimal price = decimal.Parse(this.TextBox6.Text);
GoodsDBEntities db = new GoodsDBEntities();
Good good = db.Goods.SingleOrDefault(x => x.GoodID == id);
if (good != null) {
good.GoodPrice = price;
db.Entry(good).State = System.Data.EntityState.Modified;
db.SaveChanges();
List<Good> list = db.Goods.ToList();
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
}
所有代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.Models;
namespace WebApplication1
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
GoodsDBEntities db = new GoodsDBEntities();
List<Good> list = db.Goods.ToList();
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string text = this.TextBox1.Text;
GoodsDBEntities db = new GoodsDBEntities();
List<Good> list = db.Goods.Where(m => m.GoodName.Contains(text)).ToList();
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
int id = int.Parse(this.TextBox2.Text);
GoodsDBEntities db = new GoodsDBEntities();
Good model = new Good();
model.GoodID = id;
db.Entry(model).State = System.Data.EntityState.Deleted;
db.Goods.Remove(model);
db.SaveChanges();
List<Good> list = db.Goods.ToList();
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
string name = this.TextBox3.Text;
decimal price = decimal.Parse(this.TextBox4.Text);
Good model = new Good();
model.GoodName = name;
model.GoodPrice = price;
GoodsDBEntities db = new GoodsDBEntities();
db.Goods.Add(model);
db.SaveChanges();
List<Good> list = db.Goods.ToList();
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
protected void Button4_Click(object sender, EventArgs e)
{
int id = int.Parse(this.TextBox5.Text);
decimal price = decimal.Parse(this.TextBox6.Text);
GoodsDBEntities db = new GoodsDBEntities();
Good good = db.Goods.SingleOrDefault(x => x.GoodID == id);
if (good != null) {
good.GoodPrice = price;
db.Entry(good).State = System.Data.EntityState.Modified;
db.SaveChanges();
List<Good> list = db.Goods.ToList();
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
}
}
}