编写 WinFons 应用程序,管理学生的基本信息,包括姓名、年龄和成绩。请使用数据库优先的方式创建数据库并生成实体,然后在 WinForms 应用程序中,使用 LINO 查询和操作这些学生信息。要求如下。
(1)使用数据库优先方式创建一个名为Stdent的数据库表,包含姓名、年龄和成绩字段。
(2)使用 Entity Framework Core 将数据库表映射到实体类,
(3)创建一个 WinFonms 界面,显示学生信息的列表,并包含添加、编辑和删除学生亻息的功能。
(4)使用LINQ 查询学生信息,并在界面上显示和操作这些信息。
已经写了一大半了,但是现在bug好多好多
public partial class SY7 : Form
{
private readonly DBContext c = new DBContext();
public SY7()
{
InitializeComponent();
button1.Click += button1_Click;
button2.Click += button2_Click;
studentBindingSource.AddingNew += StudentBindingSource_AddingNew;
InitStudentData();
LoadStudentData();
}
private void StudentBindingSource_AddingNew(object? sender, System.ComponentModel.AddingNewEventArgs e)
{
var newStudent = new Student();
c.Student.Add(newStudent);
e.NewObject = newStudent;
}
private void button2_Click(object sender, EventArgs e)
{
c.SaveChanges();
studentBindingSource.EndEdit();
MessageBox.Show("保存成功");
}
private void button1_Click(object sender, EventArgs e)
{
studentBindingSource.RemoveCurrent();
studentBindingSource.EndEdit();
c.SaveChanges();
}
private void InitStudentData()
{
var n = c.Student.Count();
if (n == 0)
{
c.Student.Add(new Student { Name = "张三", Age = 20, Score = 90 });
c.SaveChanges();
}
}
private void LoadStudentData()
{
var students = c.Student.ToList();
studentBindingSource.DataSource = students;
}
}
数据库的建立代码为
CREATE TABLE [dbo].[Student] (
[Id] INT NOT NULL,
[Name] NVARCHAR (50) NULL,
[Age] INT NULL,
[Score] INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
然后界面是
界面是这样的
现在bug有点多 自己输入的数据保存不到数据库中 然后最开始的那个数据在winform中不显示