2012-07-15 42 views
7

que había creado este método para comprobar el número de este registro en la tabla pero me da este mensaje de error cuando el valor de la cuenta (*) es 0 i utilizar esta biblioteca para conectar base de datos OracleLa operación no es válida debido al estado actual del objeto. en C#

utilizando Oracle.DataAccess.Client;

private int checkPort(int portID) 
     { 
      int intCount = 0; 
      try 
      { 
       OracleCommand oraCommand = new OracleCommand(); 
       oraCommand.Connection = new DBManager().getConnection(); 
       oraCommand.CommandText = "select count(*) as num from wireless_port_oid where port_id=:port_id"; 
       oraCommand.Parameters.Add(":port_id", portID); 

       OracleDataReader Reader= oraCommand.ExecuteReader(); 


       return intCount; 
       while (**Reader.Read()**)//it gives exception here 
//The err Operation is not valid due to the current state of the object. 
       { 
        intCount =Convert.ToInt32(Reader[0]); 
        Reader.Close(); 
        oraCommand.Connection.Close(); 
        oraCommand = null; 
        if (intCount > 0) 
        { 
         return 1; 
        } 
       } 
       Reader.Close(); 
       Reader.Dispose(); 
       oraCommand.Connection.Close(); 
       oraCommand.Connection.Dispose(); 
       oraCommand.Dispose(); 
       return 0; 

      } 
      catch (OracleException exception) 
      { 
       Console.WriteLine(exception.Message); 
       return 0; 
      } 
     } 
+2

lo que el retorno se intCount haciendo allí antes de que el tiempo? – Thousand

+1

algunas sugerencias: utilice [ExecuteScalar] (http://msdn.microsoft.com/en-us/library/system.data.oracleclient.oraclecommand.executescalar%28v=vs.71%29.aspx) para obtener una sola valor (no es necesario iterar/usar lectura). Y debe usar el statmenet 'using' para que su comando, etc. se elimine automáticamente. –

Respuesta

4

Usted está cerrando el lector en el Cargo = 0 y luego tratar de leerlo de nuevo en el bucle while.

while (Reader.Read())//it gives exception here 
//The err Operation is not valid due to the current state of the object. 
       { 
        intCount =Convert.ToInt32(Reader[0]); 
        Reader.Close(); 
        oraCommand.Connection.Close(); 
        oraCommand = null; 
        if (intCount > 0) 
        { 
         return 1; 
        } 
        // if intCOunt == 0 then what? loop again 
       } 

Pero el código no es válido - Me acabo de dar cuenta de que tiene una intCount retorno; justo antes de la línea que dice tiene un error. Supongo que es solo un error tipográfico de ejemplo.

Me refactorizar el código para tomar adavantage de instrucción using C# 's:

private int checkPort(int portID) { 
    string sql = "select count(*) as num from wireless_port_oid where port_id=:port_id"; 
    int intCount = 0; 
    try { 
     using(OracleCommand oraCommand = new OracleCommand()) { 
      using(oraCommand.Connection = new DBManager().getConnection()) { 
       oraCommand.CommandText = sql; 
       oraCommand.Parameters.Add(":port_id", portID); 
       intCount = oraCommand.ExecuteScalar(); 
      } 
     } 
    } 
    catch (OracleException exception) { 
     Console.WriteLine(exception.Message); 
      // may be you shouldn't return 0 here possibly throw; 
    } 

    return intCount; 
} 
+0

gracias esto me ayudó – danarj

Cuestiones relacionadas