Liangjuhan 2022-05-18 13:12 采纳率: 50%
浏览 67
已结题

想做一个成绩等级分类查询表,但是SQL语句在数据库中可以运行,但是放入程序中运行就会报错,不知道是什么问题,希望大家帮我看看

问题遇到的现象和发生背景

img

img

问题相关代码,请勿粘贴截图
        private void button1_Click(object sender, EventArgs e)
        {
            string rank = comboBox1.Text;
            string cid = textBox16.Text;
            string ConStr = "server=.;Data Source=LAPTOP-2SRQF5GA;Initial Catalog=JWXT;Integrated Security=True";      //连接字符串   
            SqlConnection conn = new SqlConnection(ConStr);
            string sqlstr = "select S.NAME 学生姓名,C.NAME 课程名称,SC.SCORE 课程成绩," +
                "case when SC.SCORE<60 then '不及格' " +
                "when SC.SCORE>=60 and SC.SCORE<80 then '良好' " +
                "when SC.SCORE>=80 and SC.SCORE<=100 then '优秀' " +
                "end as 成绩等级 from S,C,SC " +
                "where S.ID=SC.SID and C.ID=SC.CID";
            string sqlstr1 = "select S.NAME 学生姓名,C.NAME 课程名称,SC.SCORE 课程成绩," +
                "case when SC.SCORE<60 then '不及格' " +
                "when SC.SCORE>=60 and SC.SCORE<80 then '良好' " +
                "when SC.SCORE>=80 and SC.SCORE<=100 then '优秀' " +
                "end as 成绩等级 from S,C,SC " +
                "where S.ID=SC.SID and C.ID=SC.CID and C.ID='" + cid + "'";
            string sqlstr2 = "if Object_Id('QA') is not null   " +
                "drop table QA" +
                "select S.NAME 学生姓名, C.NAME 课程名称, SC.SCORE 课程成绩," +
                "case when SC.SCORE < 60 then '不及格' when SC.SCORE >= 60 and SC.SCORE < 80 then '良好'" +
                "when SC.SCORE >= 80 and SC.SCORE <= 100 then '优秀'" +
                "end as 成绩等级" +
                "into QA" +
                "from S, C, SC" +
                "where S.ID = SC.SID and C.ID = SC.CID" +
                "select* from QA where 成绩等级 = '" + rank + "'";
            string sqlstr3 = "if Object_Id('QA') is not null   " +
                "drop table QA" +
                "select S.NAME 学生姓名, C.NAME 课程名称, SC.SCORE 课程成绩," +
                "case when SC.SCORE < 60 then '不及格' when SC.SCORE >= 60 and SC.SCORE < 80 then '良好'" +
                "when SC.SCORE >= 80 and SC.SCORE <= 100 then '优秀'" +
                "end as 成绩等级" +
                "into QA" +
                "from S, C, SC" +
                "where S.ID = SC.SID and C.ID = SC.CID and C.ID='" + cid + "'" +
                "select* from QA where 成绩等级 = '" + rank + "'";
            SqlDataAdapter sda = new SqlDataAdapter();
            DataSet ds = new DataSet();
            if (rank.Length == 0)
            {
                if (cid.Length == 0)
                {
                    sda = new SqlDataAdapter(sqlstr, conn);
                    ds = new DataSet();
                    sda.Fill(ds);
                    this.dataGridView1.DataSource = ds.Tables[0];
                    MessageBox.Show("查询成功!");
                }
                else
                {
                    sda = new SqlDataAdapter(sqlstr1, conn);
                    ds = new DataSet();
                    sda.Fill(ds);
                    this.dataGridView1.DataSource = ds.Tables[0];
                    MessageBox.Show("查询成功!");
                }
            }
            else
            {
                if (cid.Length == 0)
                {
                    sda = new SqlDataAdapter(sqlstr2, conn);
                    ds = new DataSet();
                    sda.Fill(ds);
                    this.dataGridView1.DataSource = ds.Tables[0];
                    MessageBox.Show("查询成功!");
                }
                else
                {
                    sda = new SqlDataAdapter(sqlstr3, conn);
                    ds = new DataSet();
                    sda.Fill(ds);
                    this.dataGridView1.DataSource = ds.Tables[0];
                    MessageBox.Show("查询成功!");
                }
            }
        }


运行结果及报错内容

img

img


我不知道“S”是哪里的问题。

我的解答思路和尝试过的方法
我想要达到的结果

img

  • 写回答

2条回答 默认 最新

  • 渔不是鱼 2022-05-18 21:05
    关注

    你把代码中的
    "if Object_Id('QA') is not null " +
    "drop table QA"
    这个东西去掉 你试试。应该就可以了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月31日
  • 已采纳回答 5月23日
  • 创建了问题 5月18日