Reader 为空

Reader is empty

我有一个数据 reader 结果为空,但我不明白为什么?

我的连接状态是打开的,我在我的数据库中测试了我的查询,它 returns 值但是我的 reader.read() 被跳过了

OdbcDataReader Reader;
string mond = "select fir_mdnt from safir JOIN safirbpv ON firbpv_fir = fir_fir where firbpv_ibnr = '" + _cb_Compte.SelectedItem + "' group by fir_mdnt;";
OdbcCommand mondant = new OdbcCommand(mond, InfoConnexion);

InfoConnexion.Open();
Reader = mondant.ExecuteReader();
while (Reader.Read())
{
    MessageBox.Show(Reader.ToString());
}
InfoConnexion.Close();

复习例程并在using (OdbcDataReader reader = mondant.ExecuteReader()) {

上设置断点

代码已清除

//DONE: make SQL readable
//DONE: make SQL parametrized (ODBC doesn't support named parameters, but ?)
string sql = 
    @"select fir_mdnt 
        from safir join 
             safirbpv on firbpv_fir = fir_fir 
       where firbpv_ibnr = ?
    group by fir_mdnt";

//DONE: wrap IDisposable into using
//DONE: do not share the connection but create a new one
using (OdbcConnection con = new OdbcConnection(connectionStringHere)) {
  con.Open();

  //DONE: wrap IDisposable into using
  using(OdbcCommand mondant = new OdbcCommand(sql, con)) {
    //TODO: check the actual parameter type and value
    //TODO: you may want to add Trim: _cb_Compte.SelectedItem.ToString().Trim() 
    mondant.Parameters.Add(new OdbcParameter("", OdbcType.Text) 
      {Value = _cb_Compte.SelectedItem}); 

    //DONE: wrap IDisposable into using
    //TODO: put a break point here
    using (OdbcDataReader reader = mondant.ExecuteReader()) {
      while (reader.Read()) {
        MessageBox.Show(Convert.ToString(reader[0]));
      }
    }
  } 
}

然后你必须调试

  • 运行 代码。
  • 停在断点
  • 检查sql,参数(它的值)。
  • 检查 reader.HasRows 值。
  • 在任何类型的 RDMBS 编辑器上执行查询:是否有任何返回的记录?