1、MVC EF6的增删改成小练习

namespace T4Demo
{
public partial class Form1 : Form
{
NBEntities nb = new NBEntities(); public Form1()
{
InitializeComponent();
} //新增数据
private void button1_Click(object sender, EventArgs e)
{
try
{
Sys_Log sl = new Sys_Log();
sl.F_Id = "120321023213fd0";
sl.F_Account = "super";
sl.F_NickName = "超级管理员";
sl.F_Type = "Login";
sl.F_Date = DateTime.Now;
sl.F_IPAddress = "192.168.1.1";
sl.F_IPAddressName = "江苏电信";
sl.F_ModuleName = "系统登录";
sl.F_CreatorTime = DateTime.Now;
sl.F_CreatorUserId = "";
sl.F_Result = true;
sl.F_Description = "dengluchenggong";
sl.F_CreatorUserId = "";
sl.F_CreatorTime = DateTime.Now; nb.Sys_Log.Add(sl); //内存上面的操作
nb.SaveChanges();
}
catch(Exception ex) { } } //查询
private void button2_Click(object sender, EventArgs e)
{
List<Sys_Log> ds = nb.Sys_Log.Where(p => p.F_Account == "super").ToList();
this.dataGridView1.DataSource = ds;
} //修改数据
private void button3_Click(object sender, EventArgs e)
{
//查询要修改的数据
//(1)单行修改
//Sys_Log pEdit = nb.Sys_Log.Where(p=>p.F_Account== "super").FirstOrDefault();
        item.F_Description = "内容被修改了";
        item.F_IPAddress = "192.168.0.0";
         nb.SaveChanges();
            //(2)批量修改
List<Sys_Log> pEdit = nb.Sys_Log.Where(p=>p.F_Account=="super").ToList();
//保存修改 foreach (Sys_Log item in pEdit)
{
item.F_Description = "内容被修改了";
item.F_IPAddress = "192.168.0.0";
}
nb.SaveChanges(); } //删除数据
private void button4_Click(object sender, EventArgs e)
{
//(1)先查后删 根据ID删除
Sys_Log sl = nb.Sys_Log.Where(p => p.F_Id == "1203210232100").FirstOrDefault();
nb.Sys_Log.Remove(sl);
nb.SaveChanges();
//(2)自己手动添加
Sys_Log sls = new Sys_Log() { F_Id= "120321023213fd0" };
//附件到实体上下文
nb.Sys_Log.Attach(sls);
nb.Sys_Log.Remove(sls);
nb.SaveChanges();
}
}
}
05-08 08:42