用c#与mysql建立联系,制作学生选课的程序
时间: 2024-02-28 13:54:04 浏览: 56
好的,我可以为你提供一些指导。首先,你需要安装MySQL数据库,并在其中创建一个包含学生和课程信息的表。然后,你需要使用C#编写代码,以便连接到该数据库并执行查询,以显示和更新选课信息。
以下是一个简单的示例代码,它可以连接到MySQL数据库,并获取学生和课程信息:
```
using System;
using MySql.Data.MySqlClient;
namespace StudentCourse
{
class Program
{
static void Main(string[] args)
{
string connectionString = "SERVER=localhost;DATABASE=student_course;UID=user;PASSWORD=password;";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command = connection.CreateCommand();
try
{
connection.Open();
command.CommandText = "SELECT * FROM student";
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["id"] + ", " + reader["name"]);
}
reader.Close();
command.CommandText = "SELECT * FROM course";
reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["id"] + ", " + reader["name"]);
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
Console.ReadLine();
}
}
}
```
在这个示例中,我们连接到名为student_course的数据库,并在其中执行SELECT语句,以获取学生和课程信息。你可以根据你的需求修改这个示例代码,以满足你的要求。
希望这个指导可以帮助你开始制作学生选课的程序。如果你有任何问题或需要更多的帮助,请随时问我。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)