1

嗨,我有以下三个表

在此处输入图像描述

我想获得用户通过的课程的 AGPA

在此处输入图像描述

我使用了这个 SQL 语法

string queryString = 
     "select R.mark, c.NumberOfCredits 
      from Courses c, RegisteredIn R 
      where R.CourseId=c.id and R.StudentId=StudentId and 
            R.mark > 60 R.mark themark,c.NumberOfCredits credit 
            select (sum(themark*credit)/sum(credit))*0.04 AGPA    ";

我这样做是为了打印出结果

using (SqlConnection connection = new SqlConnection(connectionString)){
        SqlCommand command = new SqlCommand(queryString, connection);

        connection.Open();
        System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader();
        reader.Read();

        result = string.Empty;

        _AGPA = reader[3].ToString();

        result += string.Format("Your GPA : {0} </br>  ",_AGPA);

       Response.Write(result);
       reader.Close();
 }

我很确定 SQL 语法是错误的 我收到这个错误

在此处输入图像描述

请告知如何解决它以及我在哪里做错了。

谢谢


更新 :

数据类型如下和我使用 SQL Server 数据库 在此处输入图像描述


更新 2:

StudentId 通过页面的 URL 传递,如下所示

http://localhost:3401/MobileWebWIthConnection/showagpa.aspx?StudentId=20111

在我读的代码中

string StudentId = Request.Params["StudentId"];
4

2 回答 2

2

试试这个查询,它返回 AGPA for StudentId

select (sum(cast(R.mark as numeric) * cast(c.NumberOfCredits as numeric)) / sum(cast(c.NumberOfCredits as numeric)))*0.04 AGPA
from Courses c join RegisteredIn R 
on R.CourseId = c.id 
where R.StudentId = @studentId and R.mark > 60

在你的线路之后

SqlCommand command = new SqlCommand(queryString, connection);

添加

command.Parameters.AddWithValue("studentId", YOUR_VARIABLE_FROM_URL);
于 2012-05-04T17:38:36.380 回答
0

The problem is here: "and R.mark > 60 R.mark themark" are you missing an operator or something?

My guess is that you just need to remove the "R.mark the mark" after > 60

于 2012-05-04T17:34:57.157 回答