我正在開發一個Web應用程序,通過電子郵件向所有用戶發送測驗。如果數據庫中有多個測驗未發送給用戶,系統應該選擇最小測驗ID而不是最大測驗ID。爲什麼我在SQL查詢中使用MIN時系統中出現錯誤?
string quizid = "";
// Open DB connection.
conn.Open();
string cmdText = "SELECT MIN (QuizID) FROM dbo.QUIZ WHERE IsSent <> 1";
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
// There is only 1 column,
// so just retrieve it using the ordinal position
quizid = reader["QuizID"].ToString();
}
}
reader.Close();
}
當我用MIN,它給了我下面的錯誤:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeException: QuizID
Source Error:
> Line 69: {
> Line 70: //
> There is only 1 column, so just retrieve it using the ordinal position
> Line 71: quizid = reader["QuizID"].ToString();
> Line 72:
> Line 73: }
'reader'永遠不會爲空。 – SLaks 2012-01-01 12:46:19