AI智能
改变未来

ASP.NET EntityFramework 实现简单的增删改查操作

ASP.NET EntityFramework 实现简单的增删改查操作–许俊活
首先第一步引入EF框架,点击项目选择添加然后新建项

然后就是搭建三层,我这边没用类库,各位见谅,就用了三个文件夹

首先我们先进行Web.config的配置

<configSections><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><section name=\"entityFramework\" type=\"System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" requirePermission=\"false\"/></configSections><connectionStrings><add name=\"FreshLiveEntities\" connectionString=\"metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=FreshLive;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;\" providerName=\"System.Data.EntityClient\"/></connectionStrings><entityFramework><defaultConnectionFactory type=\"System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework\"><parameters><parameter value=\"mssqllocaldb\"/></parameters></defaultConnectionFactory><providers><provider invariantName=\"System.Data.SqlClient\" type=\"System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer\"/></providers></entityFramework></configuration>

查的功能
index.aspx的代码

<form id=\"form1\" runat=\"server\"><div><asp:GridView ID=\"GridView1\" DataKeyNames=\"UserID\" runat=\"server\" AutoGenerateColumns=\"False\" OnRowDeleting=\"GridView1_RowDeleting\" ><Columns><asp:BoundField DataField=\"Userid\" HeaderText=\"用户编号\" /><asp:TemplateField HeaderText=\"用户姓名\"><ItemTemplate><a href=\'UpdateUser.aspx?id=<%#Eval(\"UserID\") %>\'><%#Eval(\"UserName\") %></a></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText=\"操作\"><ItemTemplate><a href=\'UpdateUser.aspx?id=<%#Eval(\"UserID\") %>\'>编辑</a><asp:LinkButton ID=\"LinkButton1\" CommandName=\"Delete\" runat=\"server\">删除</asp:LinkButton></ItemTemplate></asp:TemplateField></Columns></asp:GridView><asp:Button ID=\"Button1\" runat=\"server\" Text=\"添加信息\" OnClick=\"Button1_Click\" /></div></form>

index.aspx.cs的代码

protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){Binder();}}public void Binder(){UserBLL ub = new UserBLL();GridView1.DataSource = ub.GetUsers();GridView1.DataBind();}protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){string id = GridView1.DataKeys[e.RowIndex].Value.ToString();UserBLL ub = new UserBLL();string meg = ub.DeleteUsers(int.Parse(id));this.ClientScript.RegisterStartupScript(this.GetType(), \"meg\", \"<script>alert(\'\" + meg + \"\')</script>\");Binder();}protected void Button1_Click(object sender, EventArgs e){Response.Redirect(\"addweb.aspx\");}

add.aspx的代码

<form id=\"form1\" runat=\"server\"><div><asp:TextBox ID=\"TextBox1\" runat=\"server\"></asp:TextBox><asp:TextBox ID=\"TextBox2\" runat=\"server\"></asp:TextBox><asp:Button ID=\"Button1\" runat=\"server\" Text=\"操作\" OnClick=\"Button1_Click\" /></div></form>

add.aspx.cs的代码

protected void Page_Load(object sender, EventArgs e){}protected void Button1_Click(object sender, EventArgs e){UserInfo ui = new UserInfo();ui.UserName = TextBox1.Text;ui.UserPwd = TextBox2.Text;UserBLL ub = new UserBLL();string meg = ub.AddUsers(ui);this.ClientScript.RegisterStartupScript(this.GetType(), \"meg\", \"<script>alert(\'\" + meg + \"\')</script>\");}

Update.aspx的代码

<form id=\"form1\" runat=\"server\"><div><asp:DropDownList ID=\"DropDownList1\" runat=\"server\"></asp:DropDownList><asp:TextBox ID=\"TextBox2\" runat=\"server\"></asp:TextBox><asp:TextBox ID=\"TextBox1\" runat=\"server\"></asp:TextBox><asp:Button ID=\"Button1\" runat=\"server\" Text=\"修改\" OnClick=\"Button1_Click\" /></div></form>

Update.aspx.cs的代码

protected void Page_Load(object sender, EventArgs e){if(!IsPostBack){Binder();string id = Request.QueryString[\"id\"].ToString();UserBLL ub = new UserBLL();UserInfo ui = ub.GetUserByID(int.Parse(id));if (ui != null){TextBox1.Text = ui.UserPwd;TextBox2.Text = ui.UserName;DropDownList1.SelectedValue = ui.UserID.ToString();}}}public void Binder(){UserBLL ub = new UserBLL();List<UserInfo> list = ub.GetUsers();DropDownList1.DataSource = list;DropDownList1.DataValueField = \"UserID\";DropDownList1.DataTextField = \"UserName\";DropDownList1.DataBind();}protected void Button1_Click(object sender, EventArgs e){string userid = DropDownList1.SelectedValue;string userpwd = TextBox1.Text;string username = TextBox2.Text;UserInfo ui = new UserInfo();ui.UserID = int.Parse(Request.QueryString[\"id\"].ToString());ui.UserName = username;ui.UserPwd = userpwd;UserBLL ub = new UserBLL();string meg = ub.UpdateUsers(ui);this.ClientScript.RegisterStartupScript(this.GetType(), \"meg\", \"<script>alert(\'\" + meg + \"\')</script>\");}

DAL代码

//查询信息public List<UserInfo> GetUsers(){FreshLiveEntities fe = new FreshLiveEntities();return fe.UserInfo.ToList();}public UserInfo GetUserByID(int id){FreshLiveEntities fe = new FreshLiveEntities();return fe.UserInfo.Find(id);}//修改public int UpdateUsers(UserInfo ui){FreshLiveEntities fe = new FreshLiveEntities();fe.Entry(ui).State = System.Data.Entity.EntityState.Modified;return fe.SaveChanges();}//删除public int DeleteUsers(int id){FreshLiveEntities fe = new FreshLiveEntities();UserInfo ui = fe.UserInfo.Find(id);fe.UserInfo.Remove(ui);return fe.SaveChanges();}//添加public int AddUsers(UserInfo ui){FreshLiveEntities fe = new FreshLiveEntities();fe.UserInfo.Add(ui);return fe.SaveChanges();}

BLL代码

public UserInfo GetUserByID(int id){UserDAL ud = new UserDAL();return ud.GetUserByID(id);}//查询信息public List<UserInfo> GetUsers(){UserDAL ud = new UserDAL();return ud.GetUsers();}//修改public string UpdateUsers(UserInfo ui){UserDAL ud = new UserDAL();int index= ud.UpdateUsers(ui);return index > 0 ? \"操作成功!\" : \"操作失败\";}//删除public string DeleteUsers(int id){UserDAL ud = new UserDAL();int index= ud.DeleteUsers(id);return index > 0 ? \"操作成功!\" : \"操作失败\";}//添加public string AddUsers(UserInfo ui){UserDAL ud = new UserDAL();int index= ud.AddUsers(ui);return index > 0 ? \"操作成功!\" : \"操作失败\";}
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » ASP.NET EntityFramework 实现简单的增删改查操作