我有一个数据读取器得到空的结果,但我不明白为什么?
我的连接状态是开放的,我在我的数据库中测试了我的查询,它会返回值,但我的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();
答案 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
值。