C# 由于对象的当前状态,操作无效。在C中#

C# 由于对象的当前状态,操作无效。在C中#,c#,oracle,C#,Oracle,我创建此方法是为了检查表中此记录的编号 但是当count(*)的值为0时,它会给我这个错误消息 我使用此库连接oracle db 使用Oracle.DataAccess.Client private int checkPort(int portID) { int intCount = 0; try { OracleCommand oraCommand = new Oracle

我创建此方法是为了检查表中此记录的编号 但是当count(*)的值为0时,它会给我这个错误消息 我使用此库连接oracle db

使用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;
            }
        }

您将在Count=0时关闭读卡器,然后尝试在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
                }
但是您的代码无效-我刚刚注意到您有一个return intCount;就在你说的那行之前有一个错误。我想这只是一个打字错误的例子

我将重构您的代码,以充分利用C#的using语句:

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;
}

在while之前,return intCount在那里做什么?一些提示:用于获取单个值(无需迭代/使用read)。您应该使用
using
statmenet,以便自动处理您的命令等。