2012-05-04 24 views
1

您好我有三個表如下所示SQL語法來獲得AGPA

enter image description here

我想要得到的AGPA爲用戶傳遞

enter image description here

我用這個課程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 "; 

a次我會這樣打印出來的結果

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(); 
} 

Im相當肯定的SQL語法錯誤 即時得到這個錯誤

enter image description here

請告知如何解決這個問題,並在那裏我做了一個錯誤。

謝謝


更新:

的數據類型是作爲follwoing &進出口使用SQL Server數據庫 enter image description here


更新2:

的StudentId是通過頁面的URL通過如下所示

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

,並在代碼中,我讀它

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

回答

2

嘗試此查詢,它returnes AGPA爲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); 
+0

我不有c.StudentId,StudentId是使用URL傳遞的,所以我使用了(R.StudentId = StudentId),但它仍然不工作,它顯示錯誤(操作數數據類型nchar對乘法運算符無效)。 – arin

+1

添加數據類型db列,我會看看我應該投什麼專欄,並且請告訴我你使用的數據庫是什麼 – igofed

+0

並且我不想要courseID或StudentID我只需要得到AGPA爲「3.33」 – arin

0

的問題是在這裏:「和R.mark> 60 R.mark themark」你缺少操作員或其他東西?

我的猜測是,你只需要去掉 「R.mark標記」 後> 60