我再次遇到另一個問題。我試圖使用DataReader從數據庫中獲取數據,但是當我測試代碼時出現錯誤。誰能幫我嗎?錯誤發生在此行:DataReader IndexOutofRangeException未被用戶代碼處理
chkAssess = readAssess [columnName] .ToString();
下面的代碼片段:
public string CheckAssess(string emailAddress, string columnName)
{
string chkAssess = "";
SqlDataReader readAssess;
//readAssess = new SqlDataReader();
string MgrAssessQry = "SELECT '"+columnName+"' FROM tblAllUsers";
//MgrAssessQry += " WHERE email ='" + emailAddress + "'";
SqlCommand cmdReadAssess = new SqlCommand(MgrAssessQry, cn);
cn.Open();
readAssess = cmdReadAssess.ExecuteReader();
while(readAssess.Read())
{
// Add the rows
chkAssess = readAssess[columnName].ToString();
}
return chkAssess;
}