我正在使用OracleDataReader将光标的元素插入列表。 游标从oracle db过程返回。问题是,当我没有在游标中返回任何元素时,应用程序将停止运行而不会给出错误。 我想知道如何检查光标是否包含元素?
这是从光标中返回的元素创建列表的功能代码,它位于c#asp.net中。
public List<VagaModel> BuscarVagas(string codCentroCusto, string anoPeriodo, string codPeriodo)
{
List<VagaModel> lstVagas = null;
using (OracleConnection con = new OracleConnection(connectionString))
{
using (OracleCommand cmd = con.CreateCommand())
{
try
{
cmd.CommandText = "NET_BUSCAR_VAGAS_P1";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("vCodCentroCusto", OracleDbType.Int64).Direction = ParameterDirection.Input;
cmd.Parameters["vCodCentroCusto"].Value = Convert.ToInt64(codCentroCusto);
cmd.Parameters.Add("vAnoPeriodo", OracleDbType.Int32).Direction = ParameterDirection.Input;
cmd.Parameters["vAnoPeriodo"].Value = Convert.ToInt32(anoPeriodo);
cmd.Parameters.Add("vCodPeriodo", OracleDbType.Int64).Direction = ParameterDirection.Input;
cmd.Parameters["vCodPeriodo"].Value = Convert.ToInt64(codPeriodo);
cmd.Parameters.Add("vCursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
OracleRefCursor cursor = (OracleRefCursor)cmd.Parameters["vCursor"].Value;
using (OracleDataReader dr = cursor.GetDataReader())
{
if (dr.HasRows)
{
lstVagas = new List<VagaModel>();
while (dr.Read())
{
var codVaga = dr.GetValue(0);
var qtdSolteiro = dr.GetValue(1);
var qtdCasal = dr.GetValue(2);
var qtdBeliche = dr.GetValue(3);
var qtdAuxiliar = dr.GetValue(4);
var capVaga = dr.GetValue(5);
var codTbVaga = dr.GetValue(6);
VagaModel vaga = new VagaModel(codVaga.ToString(), qtdSolteiro.ToString(), qtdCasal.ToString(), qtdBeliche.ToString(),
qtdAuxiliar.ToString(), capVaga.ToString(), codTbVaga.ToString());
lstVagas.Add(vaga);
}
}
//dr.Close();
//dr.Dispose();
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
return lstVagas;
}