C#学生成绩增删改查的设计与实现,要有菜单功能。数据取至数据库,如没有则建立相应的学生成绩表。
时间: 2024-06-09 18:07:15 浏览: 97
学生成绩管理系统实现增删改查
4星 · 用户满意度95%
好的,下面是C#学生成绩增删改查的设计与实现,带有菜单功能,数据取至数据库的代码示例:
首先,我们需要创建一个Windows Form应用程序,并添加一个菜单控件和一个DataGridView控件。然后,在Form_Load事件中,我们需要连接数据库并创建学生成绩表(如果不存在):
```csharp
private void Form1_Load(object sender, EventArgs e)
{
// 连接数据库
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyDatabase;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
// 创建学生成绩表(如果不存在)
string sql = "IF OBJECT_ID('dbo.StudentScore', 'U') IS NULL " +
"CREATE TABLE dbo.StudentScore " +
"(ID INT IDENTITY(1,1) PRIMARY KEY, " +
"Name VARCHAR(50) NOT NULL, " +
"Score INT NOT NULL)";
SqlCommand command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
connection.Close();
}
```
接下来,我们需要为菜单控件添加事件处理程序,以便实现学生成绩的增删改查功能。我们将使用SqlCommand和SqlDataAdapter类来执行SQL语句并获取数据:
```csharp
// 添加学生成绩
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
string name = Microsoft.VisualBasic.Interaction.InputBox("请输入学生姓名:", "添加学生成绩");
int score = int.Parse(Microsoft.VisualBasic.Interaction.InputBox("请输入学生成绩:", "添加学生成绩"));
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyDatabase;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string sql = "INSERT INTO dbo.StudentScore (Name, Score) VALUES (@Name, @Score)";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@Score", score);
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("学生成绩添加成功!", "添加学生成绩");
RefreshDataGridView();
}
// 删除学生成绩
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int id = int.Parse(Microsoft.VisualBasic.Interaction.InputBox("请输入要删除的学生成绩ID:", "删除学生成绩"));
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyDatabase;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string sql = "DELETE FROM dbo.StudentScore WHERE ID = @ID";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@ID", id);
int rowsAffected = command.ExecuteNonQuery();
connection.Close();
if (rowsAffected == 0)
{
MessageBox.Show("没有找到对应的学生成绩!", "删除学生成绩");
}
else
{
MessageBox.Show("学生成绩删除成功!", "删除学生成绩");
RefreshDataGridView();
}
}
// 修改学生成绩
private void updateToolStripMenuItem_Click(object sender, EventArgs e)
{
int id = int.Parse(Microsoft.VisualBasic.Interaction.InputBox("请输入要修改的学生成绩ID:", "修改学生成绩"));
int score = int.Parse(Microsoft.VisualBasic.Interaction.InputBox("请输入学生成绩:", "修改学生成绩"));
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyDatabase;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string sql = "UPDATE dbo.StudentScore SET Score = @Score WHERE ID = @ID";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@Score", score);
command.Parameters.AddWithValue("@ID", id);
int rowsAffected = command.ExecuteNonQuery();
connection.Close();
if (rowsAffected == 0)
{
MessageBox.Show("没有找到对应的学生成绩!", "修改学生成绩");
}
else
{
MessageBox.Show("学生成绩修改成功!", "修改学生成绩");
RefreshDataGridView();
}
}
// 查询学生成绩
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
string name = Microsoft.VisualBasic.Interaction.InputBox("请输入学生姓名:", "查询学生成绩");
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyDatabase;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string sql = "SELECT * FROM dbo.StudentScore WHERE Name = @Name";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@Name", name);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
connection.Close();
if (dataTable.Rows.Count == 0)
{
MessageBox.Show("没有找到对应的学生成绩!", "查询学生成绩");
}
else
{
dataGridView.DataSource = dataTable;
}
}
// 刷新DataGridView控件中的数据
private void RefreshDataGridView()
{
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyDatabase;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string sql = "SELECT * FROM dbo.StudentScore";
SqlCommand command = new SqlCommand(sql, connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
connection.Close();
dataGridView.DataSource = dataTable;
}
```
最后,我们需要在菜单控件的Click事件中调用相应的事件处理程序:
```csharp
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
addToolStripMenuItem_Click(sender, e);
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
deleteToolStripMenuItem_Click(sender, e);
}
private void updateToolStripMenuItem_Click(object sender, EventArgs e)
{
updateToolStripMenuItem_Click(sender, e);
}
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
searchToolStripMenuItem_Click(sender, e);
}
```
以上就是C#学生成绩增删改查的设计与实现,带有菜单功能,数据取至数据库的代码示例。
阅读全文