我有以下方法用于从数据库填充DAO。它执行3个读取操作—一个用于主对象,另两个用于某些翻译。
public bool read(string id, MySqlConnection c)
{
MySqlCommand m = new MySqlCommand(readCommand);
m.Parameters.Add(new MySqlParameter("@param1", id));
m.Connection = c;
MySqlDataReader r = m.ExecuteReader();
r.Read();
accountID = Convert.ToInt32(r.GetValue(0).ToString());
...
comment = r.GetValue(8).ToString();
r.Close();
m = new MySqlCommand(getAccountName);
m.Parameters.Add(new MySqlParameter("@param1", accountID));
m.Connection = c;
r = m.ExecuteReader();
r.Read();
account1Name = r.GetValue(0).ToString();
r.Close();
m = new MySqlCommand(getAccountName);
m.Parameters.Add(new MySqlParameter("@param1", secondAccountID));
m.Connection = c;
r = m.ExecuteReader();
r.Read();
account2Name = r.GetValue(0).ToString();
r.Close();
return true;
}
在线上
account2Name = r.GetValue(0).ToString();
我得到以下错误:
Invalid attempt to access a field before calling Read()
我不明白是什么问题-前一行叫read!