WinForm中使用EF6增删查改

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;
            }
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页