WinForm中使用EF6增删查改

3 篇文章 0 订阅
3 篇文章 0 订阅

form1.cs
在这里插入图片描述

using System;
using System.Windows.Forms;

namespace WinformAboutEF
{
    public partial class create : Form
    {
        public create()
        {
            InitializeComponent();
        }

        private void create_Load(object sender, EventArgs e)
        {

        }

        private void CreateButton_Click(object sender, EventArgs e)
        {
            CreateWin form = new CreateWin();
            form.Show();
        }

        private void AlterButton_Click(object sender, EventArgs e)
        {
            AlterWin form = new AlterWin();
            form.Show();
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            DeleteWin form = new DeleteWin();
            form.Show();
        }

        private void SearchButton_Click(object sender, EventArgs e)
        {
            FindWin form = new FindWin();
            form.Show();
        }
    }
}

CreateWin.cs
在这里插入图片描述

using System;
using System.Windows.Forms;

namespace WinformAboutEF
{
    public partial class CreateWin : Form
    {
        public CreateWin()
        {
            InitializeComponent();
        }

        private void searchnow_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != null)
            {
                using (var db = new Model1())
                {
                    var s = new Student
                    {
                        Id = int.Parse(textBox1.Text),
                        Name = textBox2.Text,
                        Age = textBox3.Text
                    };
                    db.Student.Add(s);
                    db.SaveChanges();
                    textBox4.Text = "添加成功!";
                }
            }
            else
            {
                textBox4.Text = "添加失败!";
            }
        }
    }
}

DeleteWin.cs
在这里插入图片描述

using System;
using System.Linq;
using System.Windows.Forms;

namespace WinformAboutEF
{
    public partial class DeleteWin : Form
    {
        public DeleteWin()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var db = new Model1())
            {
                string m = textBox1.Text;
                int n = int.Parse(m);
                var student = db.Student.FirstOrDefault(o => o.Id == n);
                db.Student.Remove(student);
                db.SaveChanges();
            }
        }
    }
}

FindWin.cs
在这里插入图片描述

using System;
using System.Linq;
using System.Windows.Forms;

namespace WinformAboutEF
{
    public partial class FindWin : Form
    {
        public FindWin()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var db = new Model1())
            {
                var i = IdBox.Text;
                int n = int.Parse(i);
                var result = db.Student.FirstOrDefault(m => m.Id == n);
                textBox1.Text = result.Name;
                textBox2.Text = result.Age;
            }
        }
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值