793291696 2018查询数据库

查询数据库

用c#连接数据库并实现可视化程序winform查询数据库。
数据库中有以下三个表
学生表:学号,姓名,性别
课程表:课程号,课程名
选课表:学号,课程号,分数

1)根据学生姓名查询其所选的课程信息

2)根据课程名查询学生姓名,分数,平均分数,分数按 降序输出

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace see2018
{
    public partial class Form1 : Form
    {
        private const string connStr = "Data Source=localhost;Initial Catalog=scnt;Integrated Security=True";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 读取输入的学生姓名
            string studentName = textBox1.Text.Trim();

            // 连接数据库,查询学生选课信息
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();

                // 构造查询语句
                string sql = @"SELECT c.name, c.id, s.name, x.score
                                FROM score x
                                JOIN student s ON x.student_id = s.id
                                JOIN course c ON x.course_id = c.id
                                WHERE s.name = @studentName";

                // 构造查询命令对象
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@studentName", studentName);

                    // 执行查询,得到查询结果
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dataTable = new DataTable();
                    adapter.Fill(dataTable);

                    dataGridView1.DataSource = dataTable;
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string coursename=comboBox1.Text.Trim();
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();

                // 构造查询语句
                string sql = @"SELECT s.name, x.score
                                FROM score x
                                JOIN student s ON x.student_id = s.id
                                JOIN course c ON x.course_id = c.id
                                WHERE c.name = @courseName
                                ORDER BY x.score DESC";

                // 构造查询命令对象
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@courseName", coursename);

                    // 执行查询,得到查询结果
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dataTable = new DataTable();
                    adapter.Fill(dataTable);


                    double sum = 0;
                    foreach (DataRow row in dataTable.Rows)
                    {
                        sum += Convert.ToDouble(row["score"]);
                    }
                    double avgScore = sum / dataTable.Rows.Count;

                    // Add a new row to show the average score
                    DataRow newRow = dataTable.NewRow();
                    newRow["name"] = "Average Score";
                    newRow["score"] = avgScore;
                    dataTable.Rows.Add(newRow);

                    dataGridView2.DataSource = dataTable;
                }
            }
        }
    }
}

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值