Invalid attempt to access field before calling read()

前端 未结 2 1806
生来不讨喜
生来不讨喜 2021-01-19 01:18

I have searched for a good couple hours now, looking for a solution to this problem. I am trying to get information from my database using the code below with the correct qu

2条回答
  •  梦谈多话
    2021-01-19 01:28

    Not sure if this is the problem in your case, but you should always check the result of Read(). eg

    if (myReader.Read())
    {
      statement = myReader.GetString(0);
    }
    

    Edit: Also what you are actually doing is retrieving a scalar, and as such you could use ExecuteScalar()

    return (myCommand.ExecuteScalar() ?? string.Empty).ToString();
    //also rename your method appropriately
    

提交回复
热议问题