读者是空的

时间:2017-11-16 13:28:23

标签: c# sql odbc datareader

我有一个数据读取器得到空的结果,但我不明白为什么?

我的连接状态是开放的,我在我的数据库中测试了我的查询,它会返回值,但我的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();

1 个答案:

答案 0 :(得分:2)

刷新例程并在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编辑器上执行查询:您是否返回了任何记录?